未验证 提交 c3d1e9e9 编写于 作者: C Chason Tang 提交者: GitHub

feat(iOS): Complete TCP mock logic. (#1053)

1. Fix xPath become nil error.
上级 23c7bc0b
......@@ -34,7 +34,9 @@ typedef NS_ENUM(NSUInteger, DKMultiControlStreamManagerState) {
/// @brief Main thread.
@interface DKMultiControlStreamManager : NSObject
@property(nonatomic, nullable, copy) NSString *_Nullable (^searchIdConstructor)(NSURL *url);
@property(nonatomic, nullable, copy) NSString *(^searchIdConstructor)(NSURL *url);
@property(nonatomic, nullable, copy) void (^tcpHandler)(NSString *_Nullable message);
@property(readonly) DKMultiControlStreamManagerState state;
......@@ -60,6 +62,8 @@ typedef NS_ENUM(NSUInteger, DKMultiControlStreamManagerState) {
- (void)broadcastWithActionMessage:(NSString *)message;
- (void)broadcastWithTCPMessage:(nullable NSString *)message;
@end
NS_ASSUME_NONNULL_END
......@@ -29,8 +29,6 @@ static NSString *generateId(void);
static NSString *const MULTI_CONTROL_HOST = @"mc_host";
static NSString *const MULTI_CONTROL_ACTION = @"action";
//static NSString *const BEHAVIOR_ID = @"68753A444D6F12269C600050E4C00067";
@interface DKMultiControlStreamManager ()
......@@ -89,7 +87,7 @@ NSString *generateId(void) {
typeof(weakSelf) self = weakSelf;
if ([commonDTOModel.dataType isEqualToString:MULTI_CONTROL_HOST]) {
[self changeToSlave];
} else if ([commonDTOModel.dataType isEqualToString:MULTI_CONTROL_ACTION]) {
} else if ([commonDTOModel.dataType isEqualToString:DK_ACTION]) {
// Handle behaviorId and process data.
NSData *jsonData = [commonDTOModel.data dataUsingEncoding:NSUTF8StringEncoding];
if (jsonData) {
......@@ -103,6 +101,8 @@ NSString *generateId(void) {
}
}
}
} else if ([commonDTOModel.dataType isEqualToString:DK_TCP]) {
self.tcpHandler ? self.tcpHandler(commonDTOModel.data) : nil;
}
};
for (id <DKMultiControlStreamManagerStateListener> listener in self.listenerArray) {
......@@ -395,4 +395,28 @@ NSString *generateId(void) {
[self.webSocketSession sendString:dataString requestId:nil completionHandler:nil];
}
- (void)broadcastWithTCPMessage:(NSString *)message {
if (!self.webSocketSession) {
return;
}
DKCommonDTOModel *commonDTOModel = [[DKCommonDTOModel alloc] init];
commonDTOModel.requestId = nil;
commonDTOModel.deviceType = DK_DEVICE_TYPE;
commonDTOModel.data = message;
commonDTOModel.method = DK_WEBSOCKET_BROADCAST;
commonDTOModel.connectSerial = self.webSocketSession.sessionUUID;
commonDTOModel.dataType = DK_ACTION;
NSError *error = nil;
NSDictionary *jsonDictionary = [MTLJSONAdapter JSONDictionaryFromModel:commonDTOModel error:&error];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary ?: @{} options:0 error:&error];
NSString *dataString = nil;
if (jsonData) {
dataString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
if (!dataString) {
return;
}
[self.webSocketSession sendString:dataString requestId:nil completionHandler:nil];
}
@end
......@@ -32,6 +32,8 @@ extern NSString *DK_DATA_QUERY;
extern NSString *DK_ACTION;
extern NSString *DK_TCP;
@interface DKCommonDTOModel : MTLModel <MTLJSONSerializing>
@property(nonatomic, nullable, copy) NSNumber *requestId;
......
......@@ -30,6 +30,8 @@ NSString *DK_DATA_QUERY = @"query";
NSString *DK_ACTION = @"action";
NSString *DK_TCP = @"tcp";
@implementation DKCommonDTOModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
......
......@@ -29,6 +29,10 @@ static NSString const *kcustomTypeKey =@"customType";
DoraemonMCMessage *messageInstance = [[DoraemonMCMessage alloc] init];
messageInstance.type = type;
DoraemonMCXPathSerializer *xPathInstance = [DoraemonMCXPathSerializer xPathInstanceWithView:view];
if (xPathInstance.windowIndex == NSNotFound) {
// 如果存在埋点 SDK,埋点会被后调用,先前调用的业务 action 关闭了当前页面,导致 sender 不存在于视图中,会出现这种情况。这种情况需要过滤
return nil;
}
if (xPathInstance.ignore) {
return nil;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册