Skip to content

Commit c3d1e9e

Browse files
authoredJun 24, 2022
feat(iOS): Complete TCP mock logic. (#1053)
1. Fix xPath become nil error.
1 parent 23c7bc0 commit c3d1e9e

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed
 

‎iOS/DoKit/Classes/Foundation/DKMultiControlStreamManager.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ typedef NS_ENUM(NSUInteger, DKMultiControlStreamManagerState) {
3434
/// @brief Main thread.
3535
@interface DKMultiControlStreamManager : NSObject
3636

37-
@property(nonatomic, nullable, copy) NSString *_Nullable (^searchIdConstructor)(NSURL *url);
37+
@property(nonatomic, nullable, copy) NSString *(^searchIdConstructor)(NSURL *url);
38+
39+
@property(nonatomic, nullable, copy) void (^tcpHandler)(NSString *_Nullable message);
3840

3941
@property(readonly) DKMultiControlStreamManagerState state;
4042

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

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

65+
- (void)broadcastWithTCPMessage:(nullable NSString *)message;
66+
6367
@end
6468

6569
NS_ASSUME_NONNULL_END

‎iOS/DoKit/Classes/Foundation/DKMultiControlStreamManager.m

+27-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
static NSString *const MULTI_CONTROL_HOST = @"mc_host";
3131

32-
static NSString *const MULTI_CONTROL_ACTION = @"action";
33-
3432
//static NSString *const BEHAVIOR_ID = @"68753A444D6F12269C600050E4C00067";
3533

3634
@interface DKMultiControlStreamManager ()
@@ -89,7 +87,7 @@ - (void)enableMultiControlWithUrl:(NSURL *)url {
8987
typeof(weakSelf) self = weakSelf;
9088
if ([commonDTOModel.dataType isEqualToString:MULTI_CONTROL_HOST]) {
9189
[self changeToSlave];
92-
} else if ([commonDTOModel.dataType isEqualToString:MULTI_CONTROL_ACTION]) {
90+
} else if ([commonDTOModel.dataType isEqualToString:DK_ACTION]) {
9391
// Handle behaviorId and process data.
9492
NSData *jsonData = [commonDTOModel.data dataUsingEncoding:NSUTF8StringEncoding];
9593
if (jsonData) {
@@ -103,6 +101,8 @@ - (void)enableMultiControlWithUrl:(NSURL *)url {
103101
}
104102
}
105103
}
104+
} else if ([commonDTOModel.dataType isEqualToString:DK_TCP]) {
105+
self.tcpHandler ? self.tcpHandler(commonDTOModel.data) : nil;
106106
}
107107
};
108108
for (id <DKMultiControlStreamManagerStateListener> listener in self.listenerArray) {
@@ -395,4 +395,28 @@ - (void)broadcastWithActionMessage:(NSString *)message {
395395
[self.webSocketSession sendString:dataString requestId:nil completionHandler:nil];
396396
}
397397

398+
- (void)broadcastWithTCPMessage:(NSString *)message {
399+
if (!self.webSocketSession) {
400+
return;
401+
}
402+
DKCommonDTOModel *commonDTOModel = [[DKCommonDTOModel alloc] init];
403+
commonDTOModel.requestId = nil;
404+
commonDTOModel.deviceType = DK_DEVICE_TYPE;
405+
commonDTOModel.data = message;
406+
commonDTOModel.method = DK_WEBSOCKET_BROADCAST;
407+
commonDTOModel.connectSerial = self.webSocketSession.sessionUUID;
408+
commonDTOModel.dataType = DK_ACTION;
409+
NSError *error = nil;
410+
NSDictionary *jsonDictionary = [MTLJSONAdapter JSONDictionaryFromModel:commonDTOModel error:&error];
411+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary ?: @{} options:0 error:&error];
412+
NSString *dataString = nil;
413+
if (jsonData) {
414+
dataString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
415+
}
416+
if (!dataString) {
417+
return;
418+
}
419+
[self.webSocketSession sendString:dataString requestId:nil completionHandler:nil];
420+
}
421+
398422
@end

‎iOS/DoKit/Classes/Foundation/DTO/DKCommonDTOModel.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern NSString *DK_DATA_QUERY;
3232

3333
extern NSString *DK_ACTION;
3434

35+
extern NSString *DK_TCP;
36+
3537
@interface DKCommonDTOModel : MTLModel <MTLJSONSerializing>
3638

3739
@property(nonatomic, nullable, copy) NSNumber *requestId;

‎iOS/DoKit/Classes/Foundation/DTO/DKCommonDTOModel.m

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
NSString *DK_ACTION = @"action";
3232

33+
NSString *DK_TCP = @"tcp";
34+
3335
@implementation DKCommonDTOModel
3436

3537
+ (NSDictionary *)JSONKeyPathsByPropertyKey {

‎iOS/DoraemonKit/Src/MultiControl/Function/EventSync/MessagePackager/DoraemonMCMessagePackager.m

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ + (DoraemonMCMessage *)packageMessageWithView:(UIView *)view
2929
DoraemonMCMessage *messageInstance = [[DoraemonMCMessage alloc] init];
3030
messageInstance.type = type;
3131
DoraemonMCXPathSerializer *xPathInstance = [DoraemonMCXPathSerializer xPathInstanceWithView:view];
32+
if (xPathInstance.windowIndex == NSNotFound) {
33+
// 如果存在埋点 SDK,埋点会被后调用,先前调用的业务 action 关闭了当前页面,导致 sender 不存在于视图中,会出现这种情况。这种情况需要过滤
34+
return nil;
35+
}
3236
if (xPathInstance.ignore) {
3337
return nil;
3438
}

0 commit comments

Comments
 (0)