add dispatch_semaphore to control serial logic
This commit is contained in:
parent
08bfd71eae
commit
15dd196e79
@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
|
|
||||||
@interface DoricJSRemoteExecutor : NSObject <DoricJSExecutorProtocal>
|
@interface DoricJSRemoteExecutor : NSObject <DoricJSExecutorProtocal>
|
||||||
|
|
||||||
|
@property(nonatomic, strong) dispatch_semaphore_t semaphore;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
@ -20,44 +20,67 @@
|
|||||||
// Created by 王劲鹏 on 2019/10/31.
|
// Created by 王劲鹏 on 2019/10/31.
|
||||||
//
|
//
|
||||||
#import "DoricJSRemoteExecutor.h"
|
#import "DoricJSRemoteExecutor.h"
|
||||||
|
#import <SocketRocket/SRWebSocket.h>
|
||||||
|
#import "DoricUtil.h"
|
||||||
|
|
||||||
@interface DoricJSRemoteExecutor ()
|
@interface DoricJSRemoteExecutor () <SRWebSocketDelegate>
|
||||||
|
|
||||||
@property(nonatomic, strong) JSContext *jsContext;
|
@property(nonatomic, strong) SRWebSocket *websocket;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation DoricJSRemoteExecutor
|
@implementation DoricJSRemoteExecutor
|
||||||
- (instancetype)init {
|
- (instancetype)init {
|
||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
_jsContext = [[JSContext alloc] init];
|
_websocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://192.168.24.166:2080"]];
|
||||||
|
_websocket.delegate = self;
|
||||||
|
[_websocket open];
|
||||||
|
_semaphore = dispatch_semaphore_create(0);
|
||||||
|
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)checkJSException {
|
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
|
||||||
if (self.jsContext.exception) {
|
DoricLog(@"debugger webSocketDidOpen");
|
||||||
NSString *errMsg = [NSString stringWithFormat:@"%@ (line %@ in the generated bundle)\n/***StackTrace***/\n%@/***StackTrace***/", self.jsContext.exception, self.jsContext.exception[@"line"], self.jsContext.exception[@"stack"]];
|
dispatch_semaphore_signal(_semaphore);
|
||||||
@throw [[NSException alloc] initWithName:@"DoricJS" reason:errMsg userInfo:nil];
|
}
|
||||||
|
|
||||||
|
- (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;
|
||||||
}
|
}
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
|
- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
|
||||||
NSString *ret = [[self.jsContext evaluateScript:script withSourceURL:[NSURL URLWithString:source]] toString];
|
return nil;
|
||||||
[self checkJSException];
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
|
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
|
||||||
self.jsContext[name] = obj;
|
|
||||||
[self checkJSException];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
|
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
|
||||||
JSValue *obj = [self.jsContext objectForKeyedSubscript:objName];
|
return nil;
|
||||||
JSValue *ret = [obj invokeMethod:funcName withArguments:args];
|
|
||||||
[self checkJSException];
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
Reference in New Issue
Block a user