diff --git a/iOS/Doric.podspec b/iOS/Doric.podspec index 8f4d6e56..99e64afb 100644 --- a/iOS/Doric.podspec +++ b/iOS/Doric.podspec @@ -41,4 +41,5 @@ TODO: Add long description of the pod here. # s.frameworks = 'UIKit', 'MapKit' # s.dependency 'AFNetworking', '~> 2.3' s.dependency 'SDWebImage', '~> 5.0' + s.dependency 'SocketRocket', '~> 0.5.1' end diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 53eaaadd..18788615 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -24,9 +24,10 @@ - (void)viewDidLoad { NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"Snake" ofType:@"js"]; NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; - self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"Snake"]; + self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"test.js"]; self.doricContext.rootNode.view = self.view; [self.doricContext initContextWithWidth:self.view.width height:self.view.height]; + [self.doricContext.driver connectDevKit:@"ws://192.168.11.38:7777"]; } diff --git a/iOS/Example/Podfile.lock b/iOS/Example/Podfile.lock index 1f91e23e..7b9a85ab 100644 --- a/iOS/Example/Podfile.lock +++ b/iOS/Example/Podfile.lock @@ -1,9 +1,11 @@ PODS: - Doric (0.1.0): - SDWebImage (~> 5.0) + - SocketRocket (~> 0.5.1) - SDWebImage (5.0.6): - SDWebImage/Core (= 5.0.6) - SDWebImage/Core (5.0.6) + - SocketRocket (0.5.1) DEPENDENCIES: - Doric (from `../`) @@ -11,14 +13,16 @@ DEPENDENCIES: SPEC REPOS: https://github.com/cocoapods/specs.git: - SDWebImage + - SocketRocket EXTERNAL SOURCES: Doric: :path: "../" SPEC CHECKSUMS: - Doric: 3b2bd0260ddecb3a7ec6ed95a88ace58c2a75685 + Doric: 79268ea6e03e7dee56b072e532d9a29f50ccd72d SDWebImage: 920f1a2ff1ca8296ad34f6e0510a1ef1d70ac965 + SocketRocket: d57c7159b83c3c6655745cd15302aa24b6bae531 PODFILE CHECKSUM: 012563d71439e7e33e976dca3b59664ed56cee39 diff --git a/iOS/Pod/Classes/Dev/DoricWSClient.h b/iOS/Pod/Classes/Dev/DoricWSClient.h new file mode 100644 index 00000000..0afafd7b --- /dev/null +++ b/iOS/Pod/Classes/Dev/DoricWSClient.h @@ -0,0 +1,18 @@ +// +// WSClient.h +// Doric +// +// Created by pengfei.zhou on 2019/8/14. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricWSClient : NSObject +- (instancetype)initWithUrl:(NSString *)url; + +- (void)close; +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/Dev/DoricWSClient.m b/iOS/Pod/Classes/Dev/DoricWSClient.m new file mode 100644 index 00000000..6ead4519 --- /dev/null +++ b/iOS/Pod/Classes/Dev/DoricWSClient.m @@ -0,0 +1,65 @@ +// +// WSClient.m +// Doric +// +// Created by pengfei.zhou on 2019/8/14. +// + +#import "DoricWSClient.h" +#import +#import "DoricUtil.h" +#import "DoricContextManager.h" + +@interface DoricWSClient() +@property (nonatomic,strong) SRWebSocket *websocket; +@end + +@implementation DoricWSClient +- (instancetype)initWithUrl:(NSString *)url { + if(self = [super init]) { + _websocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:url]]; + _websocket.delegate = self; + [_websocket open]; + } + return self; +} +- (void)webSocketDidOpen:(SRWebSocket *)webSocket { + DoricLog(@"webSocketDidOpen"); +} + +- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { + DoricLog(@"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]; + NSString *script = [dic valueForKey:@"script"]; + for(NSValue *value in [[DoricContextManager instance] aliveContexts]) { + DoricContext *context = value.nonretainedObjectValue; + if([source containsString:context.source]) { + [context reload:script]; + } + } +} + +- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { + DoricLog(@"webSocketdidFailWithError"); +} + +- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { + DoricLog(@"webSocketdidCloseWithCode"); +} + +- (void)close { + [self.websocket close]; +} +@end diff --git a/iOS/Pod/Classes/DoricContextManager.h b/iOS/Pod/Classes/DoricContextManager.h index 8c1f949c..25b1e02c 100644 --- a/iOS/Pod/Classes/DoricContextManager.h +++ b/iOS/Pod/Classes/DoricContextManager.h @@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)destroyContext:(DoricContext *)context; - (DoricContext *)getContext:(NSString *)contextId; + +- (NSArray *)aliveContexts; @end NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricContextManager.m b/iOS/Pod/Classes/DoricContextManager.m index c5c437cf..f85cd44b 100644 --- a/iOS/Pod/Classes/DoricContextManager.m +++ b/iOS/Pod/Classes/DoricContextManager.m @@ -62,4 +62,8 @@ - (void)destroyContext:(DoricContext *)context { }; } +- (NSArray *)aliveContexts { + return [self.doricContextMap allValues]; +} + @end diff --git a/iOS/Pod/Classes/DoricDriver.h b/iOS/Pod/Classes/DoricDriver.h index 65461397..99b7a5fa 100644 --- a/iOS/Pod/Classes/DoricDriver.h +++ b/iOS/Pod/Classes/DoricDriver.h @@ -29,6 +29,9 @@ NS_ASSUME_NONNULL_BEGIN - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ...; - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list) args; - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args; + +- (void)connectDevKit:(NSString *)url; +- (void)disconnectDevKit; @end NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricDriver.m b/iOS/Pod/Classes/DoricDriver.m index 7a091578..9ede71d5 100644 --- a/iOS/Pod/Classes/DoricDriver.m +++ b/iOS/Pod/Classes/DoricDriver.m @@ -8,9 +8,11 @@ #import "DoricDriver.h" #import "DoricJSEngine.h" #import "DoricConstant.h" +#import "DoricWSClient.h" @interface DoricDriver() -@property (nonatomic, strong) DoricJSEngine *jsExecutor; +@property (nonatomic, strong) DoricJSEngine *jsExecutor; +@property (nonatomic, strong) DoricWSClient *wsclient; @end @implementation DoricDriver @@ -151,5 +153,18 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId { return ret; } +- (void)connectDevKit:(NSString *)url { + if(self.wsclient) { + [self.wsclient close]; + } + self.wsclient = [[DoricWSClient alloc] initWithUrl:url]; +} + +- (void)disconnectDevKit { + if(self.wsclient) { + [self.wsclient close]; + self.wsclient = nil; + } +} @end