2019-10-21 09:59:22 +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.
|
|
|
|
|
*/
|
2019-08-14 10:08:33 +08:00
|
|
|
|
//
|
|
|
|
|
// WSClient.m
|
|
|
|
|
// Doric
|
|
|
|
|
//
|
|
|
|
|
// Created by pengfei.zhou on 2019/8/14.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "DoricWSClient.h"
|
2020-10-23 16:48:44 +08:00
|
|
|
|
#import "SRWebSocket.h"
|
2019-08-14 10:08:33 +08:00
|
|
|
|
#import "DoricUtil.h"
|
|
|
|
|
#import "DoricContextManager.h"
|
|
|
|
|
|
2019-10-12 14:48:19 +08:00
|
|
|
|
@interface DoricWSClient () <SRWebSocketDelegate>
|
|
|
|
|
@property(nonatomic, strong) SRWebSocket *websocket;
|
2019-08-14 10:08:33 +08:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation DoricWSClient
|
|
|
|
|
- (instancetype)initWithUrl:(NSString *)url {
|
2019-10-12 14:48:19 +08:00
|
|
|
|
if (self = [super init]) {
|
2019-08-14 10:08:33 +08:00
|
|
|
|
_websocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:url]];
|
|
|
|
|
_websocket.delegate = self;
|
|
|
|
|
[_websocket open];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
|
|
2019-08-14 10:08:33 +08:00
|
|
|
|
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
|
|
|
|
|
DoricLog(@"webSocketDidOpen");
|
2020-02-27 16:13:14 +08:00
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"OpenEvent" object:nil];
|
2019-08-14 10:08:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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];
|
2019-10-12 14:48:19 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
DoricLog(@"webSocketdidReceiveMessage parse error:%@", err);
|
2019-08-14 10:08:33 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-27 16:13:14 +08:00
|
|
|
|
NSString *cmd = [[dic valueForKey:@"cmd"] mutableCopy];
|
|
|
|
|
if ([cmd compare:@"SWITCH_TO_DEBUG"] == NSOrderedSame) {
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"EnterDebugEvent" object:nil];
|
|
|
|
|
} else if ([cmd compare:@"RELOAD"] == NSOrderedSame) {
|
2020-02-28 17:31:59 +08:00
|
|
|
|
NSString *source = [[dic valueForKey:@"source"] mutableCopy];
|
|
|
|
|
NSString *script = [dic valueForKey:@"script"];
|
2021-02-09 15:09:52 +08:00
|
|
|
|
for (DoricContext *context in [[DoricContextManager instance] aliveContexts]) {
|
2020-02-29 10:13:53 +08:00
|
|
|
|
if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) {
|
2020-02-28 17:31:59 +08:00
|
|
|
|
[context reload:script];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-14 10:08:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
|
|
|
|
|
DoricLog(@"webSocketdidFailWithError");
|
2020-02-27 16:13:14 +08:00
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"ConnectExceptionEvent" object:nil];
|
2019-08-14 10:08:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
|
|
|
|
|
DoricLog(@"webSocketdidCloseWithCode");
|
2020-02-27 16:13:14 +08:00
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"EOFEvent" object:nil];
|
2019-08-14 10:08:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 14:51:02 +08:00
|
|
|
|
- (void)send:(NSString *)command {
|
|
|
|
|
[_websocket send:command];
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:08:33 +08:00
|
|
|
|
- (void)close {
|
|
|
|
|
[self.websocket close];
|
|
|
|
|
}
|
|
|
|
|
@end
|