Skip to content

Commit

Permalink
feat(iOS): Complete TCP mock logic. (didi#1053)
Browse files Browse the repository at this point in the history
1. Fix xPath become nil error.
  • Loading branch information
ChasonTang authored Jun 24, 2022
1 parent 23c7bc0 commit c3d1e9e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion iOS/DoKit/Classes/Foundation/DKMultiControlStreamManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -60,6 +62,8 @@ typedef NS_ENUM(NSUInteger, DKMultiControlStreamManagerState) {

- (void)broadcastWithActionMessage:(NSString *)message;

- (void)broadcastWithTCPMessage:(nullable NSString *)message;

@end

NS_ASSUME_NONNULL_END
30 changes: 27 additions & 3 deletions iOS/DoKit/Classes/Foundation/DKMultiControlStreamManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

static NSString *const MULTI_CONTROL_HOST = @"mc_host";

static NSString *const MULTI_CONTROL_ACTION = @"action";

//static NSString *const BEHAVIOR_ID = @"68753A444D6F12269C600050E4C00067";

@interface DKMultiControlStreamManager ()
Expand Down Expand Up @@ -89,7 +87,7 @@ - (void)enableMultiControlWithUrl:(NSURL *)url {
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) {
Expand All @@ -103,6 +101,8 @@ - (void)enableMultiControlWithUrl:(NSURL *)url {
}
}
}
} else if ([commonDTOModel.dataType isEqualToString:DK_TCP]) {
self.tcpHandler ? self.tcpHandler(commonDTOModel.data) : nil;
}
};
for (id <DKMultiControlStreamManagerStateListener> listener in self.listenerArray) {
Expand Down Expand Up @@ -395,4 +395,28 @@ - (void)broadcastWithActionMessage:(NSString *)message {
[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
2 changes: 2 additions & 0 deletions iOS/DoKit/Classes/Foundation/DTO/DKCommonDTOModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions iOS/DoKit/Classes/Foundation/DTO/DKCommonDTOModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

NSString *DK_ACTION = @"action";

NSString *DK_TCP = @"tcp";

@implementation DKCommonDTOModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ + (DoraemonMCMessage *)packageMessageWithView:(UIView *)view
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;
}
Expand Down

0 comments on commit c3d1e9e

Please sign in to comment.