iOS Websocket
This commit is contained in:
18
iOS/Pod/Classes/Dev/DoricWSClient.h
Normal file
18
iOS/Pod/Classes/Dev/DoricWSClient.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// WSClient.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/8/14.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricWSClient : NSObject
|
||||
- (instancetype)initWithUrl:(NSString *)url;
|
||||
|
||||
- (void)close;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
65
iOS/Pod/Classes/Dev/DoricWSClient.m
Normal file
65
iOS/Pod/Classes/Dev/DoricWSClient.m
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// WSClient.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/8/14.
|
||||
//
|
||||
|
||||
#import "DoricWSClient.h"
|
||||
#import <SocketRocket/SRWebSocket.h>
|
||||
#import "DoricUtil.h"
|
||||
#import "DoricContextManager.h"
|
||||
|
||||
@interface DoricWSClient()<SRWebSocketDelegate>
|
||||
@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
|
@@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
- (void)destroyContext:(DoricContext *)context;
|
||||
|
||||
- (DoricContext *)getContext:(NSString *)contextId;
|
||||
|
||||
- (NSArray *)aliveContexts;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -62,4 +62,8 @@ - (void)destroyContext:(DoricContext *)context {
|
||||
};
|
||||
}
|
||||
|
||||
- (NSArray *)aliveContexts {
|
||||
return [self.doricContextMap allValues];
|
||||
}
|
||||
|
||||
@end
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user