code refactor

This commit is contained in:
王劲鹏
2020-02-25 14:20:27 +08:00
committed by osborn
parent 23b0f93e89
commit 3facc3fdee
12 changed files with 147 additions and 41 deletions

View File

@@ -1,33 +0,0 @@
/*
* 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.
*/
//
// 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

View File

@@ -1,81 +0,0 @@
/*
* 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.
*/
//
// 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

View File

@@ -49,10 +49,6 @@ NS_ASSUME_NONNULL_BEGIN
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args;
- (void)connectDevKit:(NSString *)url;
- (void)disconnectDevKit;
- (void)ensureSyncInMainQueue:(dispatch_block_t)block;
- (NSString *)aliasWithContextId:(NSString *)contextId;

View File

@@ -23,12 +23,10 @@
#import "DoricDriver.h"
#import "DoricJSEngine.h"
#import "DoricConstant.h"
#import "DoricWSClient.h"
#import "DoricContextManager.h"
@interface DoricDriver ()
@property(nonatomic, strong) DoricJSEngine *jsExecutor;
@property(nonatomic, strong) DoricWSClient *wsclient;
@end
@implementation DoricDriver
@@ -178,20 +176,6 @@ - (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;
}
}
- (void)ensureSyncInMainQueue:(dispatch_block_t)block {
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {
block();