add: injectGlobalJSObject

This commit is contained in:
Insomnia 2019-11-01 00:13:50 +08:00
parent 896e9aaa51
commit af3590b0ba
3 changed files with 34 additions and 6 deletions

View File

@ -29,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
@property(nonatomic, strong) dispatch_semaphore_t semaphore; @property(nonatomic, strong) dispatch_semaphore_t semaphore;
- (void)close;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -26,8 +26,9 @@
static NSString * const kUrlStr = @"ws://192.168.24.240:2080"; static NSString * const kUrlStr = @"ws://192.168.24.240:2080";
@interface DoricJSRemoteExecutor () <SRWebSocketDelegate> @interface DoricJSRemoteExecutor () <SRWebSocketDelegate>
@property(nonatomic, strong) NSMapTable *mapTable;
@property(nonatomic, strong) SRWebSocket *websocket; @property(nonatomic, strong) SRWebSocket *websocket;
@property(nonatomic, strong) NSMapTable *mapTable;
@property(nonatomic, strong) dispatch_semaphore_t mapTableLock;
@end @end
@implementation DoricJSRemoteExecutor @implementation DoricJSRemoteExecutor
@ -35,14 +36,15 @@ - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
[self websocket]; [self websocket];
_semaphore = dispatch_semaphore_create(0); _semaphore = dispatch_semaphore_create(0);
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); _mapTableLock = dispatch_semaphore_create(1);
DC_LOCK(self.semaphore);
} }
return self; return self;
} }
- (void)webSocketDidOpen:(SRWebSocket *)webSocket { - (void)webSocketDidOpen:(SRWebSocket *)webSocket {
DoricLog(@"debugger webSocketDidOpen"); DoricLog(@"debugger webSocketDidOpen");
dispatch_semaphore_signal(_semaphore); DC_UNLOCK(self.semaphore);
} }
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload {
@ -64,7 +66,7 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
DoricLog(@"debugger webSocketdidFailWithError"); DoricLog(@"debugger webSocketdidFailWithError");
dispatch_semaphore_signal(_semaphore); DC_UNLOCK(self.semaphore);
} }
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
@ -77,7 +79,20 @@ - (NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
} }
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { - (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
DC_LOCK(self.mapTableLock);
[self.mapTable setObject:obj forKey:name];
DC_UNLOCK(self.mapTableLock);
NSDictionary *jsonDic =@{
@"cmd": @"injectGlobalJSFunction",
@"name": name
};
NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err];
if (err) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed");
}
[self.websocket send:jsonData];
} }
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
@ -105,11 +120,15 @@ - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(
} }
[self.websocket send:jsonData]; [self.websocket send:jsonData];
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); DC_LOCK(self.semaphore);
return nil; return nil;
} }
- (void)close {
[self.websocket close];
}
#pragma mark - Properties #pragma mark - Properties
- (SRWebSocket *)websocket { - (SRWebSocket *)websocket {
if (!_websocket) { if (!_websocket) {
@ -123,7 +142,7 @@ - (SRWebSocket *)websocket {
- (NSMapTable *)mapTable { - (NSMapTable *)mapTable {
if (!_mapTable) { if (!_mapTable) {
_mapTable = [NSMapTable new]; _mapTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
} }
return _mapTable; return _mapTable;
} }

View File

@ -27,3 +27,11 @@ void DoricLog(NSString *_Nonnull format, ...);
UIColor *_Nonnull DoricColor(NSNumber *_Nonnull number); UIColor *_Nonnull DoricColor(NSNumber *_Nonnull number);
NSBundle *DoricBundle(); NSBundle *DoricBundle();
#ifndef DC_LOCK
#define DC_LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
#endif
#ifndef DC_UNLOCK
#define DC_UNLOCK(lock) dispatch_semaphore_signal(lock);
#endif