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

151 lines
4.5 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) SRWebSocket *websocket;
2019-11-01 00:13:50 +08:00
@property(nonatomic, strong) NSMapTable *mapTable;
@property(nonatomic, strong) dispatch_semaphore_t mapTableLock;
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);
2019-11-01 00:13:50 +08:00
_mapTableLock = dispatch_semaphore_create(1);
DC_LOCK(self.semaphore);
2019-10-31 14:26:08 +08:00
}
return self;
}
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
DoricLog(@"debugger webSocketDidOpen");
2019-11-01 00:13:50 +08:00
DC_UNLOCK(self.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");
2019-11-01 00:13:50 +08:00
DC_UNLOCK(self.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-11-01 00:13:50 +08:00
DC_LOCK(self.mapTableLock);
[self.mapTable setObject:obj forKey:name];
DC_UNLOCK(self.mapTableLock);
2019-10-31 20:04:57 +08:00
2019-11-01 00:13:50 +08:00
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];
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
2019-10-31 23:46:10 +08:00
NSMutableArray *argsMArr = [NSMutableArray new];
for (id arg in args) {
NSDictionary *dic = @{
@"type": [arg class],
@"value": arg
};
[argsMArr addObject:dic];
}
NSDictionary *jsonDic = @{
@"cmd": @"invokeMethod",
@"obj": objName,
@"functionName": funcName,
@"javaValues": argsMArr
};
NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err];
if (err) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed");
}
[self.websocket send:jsonData];
2019-11-01 00:13:50 +08:00
DC_LOCK(self.semaphore);
2019-10-31 23:46:10 +08:00
return nil;
2019-10-31 14:26:08 +08:00
}
2019-11-01 00:13:50 +08:00
- (void)close {
[self.websocket close];
}
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) {
2019-11-01 00:13:50 +08:00
_mapTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
2019-10-31 20:04:57 +08:00
}
return _mapTable;
}
2019-10-31 14:26:08 +08:00
@end