This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m

109 lines
3.2 KiB
Mathematica
Raw Normal View History

2019-10-31 14:26:08 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// DoricJSRemoteExecutor.m
// Doric
//
// Created by on 2019/10/31.
//
#import "DoricJSRemoteExecutor.h"
#import <SocketRocket/SRWebSocket.h>
#import "DoricUtil.h"
2019-10-31 14:26:08 +08:00
2019-10-31 20:04:57 +08:00
static NSString * const kUrlStr = @"ws://192.168.24.240:2080";
2019-10-31 14:26:08 +08:00
2019-10-31 20:04:57 +08:00
@interface DoricJSRemoteExecutor () <SRWebSocketDelegate>
@property(nonatomic, strong) NSMapTable *mapTable;
@property(nonatomic, strong) SRWebSocket *websocket;
2019-10-31 14:26:08 +08:00
@end
@implementation DoricJSRemoteExecutor
- (instancetype)init {
if (self = [super init]) {
2019-10-31 20:04:57 +08:00
[self websocket];
_semaphore = dispatch_semaphore_create(0);
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
2019-10-31 14:26:08 +08:00
}
return self;
}
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
DoricLog(@"debugger webSocketDidOpen");
dispatch_semaphore_signal(_semaphore);
}
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload {
DoricLog(@"debugger webSocketdidReceivePong");
}
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
NSData *jsonData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if (err) {
DoricLog(@"webSocketdidReceiveMessage parse error%@", err);
return;
2019-10-31 14:26:08 +08:00
}
NSString *source = [[dic valueForKey:@"source"] mutableCopy];
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
DoricLog(@"debugger webSocketdidFailWithError");
dispatch_semaphore_signal(_semaphore);
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
DoricLog(@"debugger webSocketdidCloseWithCode");
2019-10-31 14:26:08 +08:00
}
- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
2019-10-31 20:04:57 +08:00
return nil;
2019-10-31 14:26:08 +08:00
}
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
2019-10-31 20:04:57 +08:00
2019-10-31 14:26:08 +08:00
}
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
2019-10-31 20:04:57 +08:00
return nil;
2019-10-31 14:26:08 +08:00
}
2019-10-31 20:04:57 +08:00
#pragma mark - Properties
- (SRWebSocket *)websocket {
if (!_websocket) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kUrlStr] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
_websocket = [[SRWebSocket alloc] initWithURLRequest:request];
_websocket.delegate = self;
[_websocket open];
}
return _websocket;
}
- (NSMapTable *)mapTable {
if (!_mapTable) {
_mapTable = [NSMapTable new];
}
return _mapTable;
}
2019-10-31 14:26:08 +08:00
@end