Skip to content

Commit

Permalink
feat: 修复数据量大的情况下收消息慢的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtaoit committed Jul 4, 2024
1 parent 8037929 commit 4e26a8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion WuKongIMSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Pod::Spec.new do |s|
}
s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }

s.dependency 'CocoaAsyncSocket', '~> 7.6.4'
s.dependency 'CocoaAsyncSocket', '~> 7.6.5'
s.dependency 'FMDB/SQLCipher', '~>2.7.5'
s.dependency '25519', '~>2.0.2'
end
32 changes: 27 additions & 5 deletions WuKongIMSDK/Classes/manager/WKConnectionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "WKConversationManagerInner.h"
#import "WKMessageQueueManager.h"


@interface WKConnectionManager ()<GCDAsyncSocketDelegate>


Expand Down Expand Up @@ -354,6 +355,14 @@ - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{

}

- (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutReadWithTag:(long)tag
elapsed:(NSTimeInterval)elapsed
bytesDone:(NSUInteger)length {

NSLog(@"read has timed out...");
return 0.0;
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *)err {
NSLog(@"IM连接已断开 -> %@",err);
self.tempBufferData = [[NSMutableData alloc] init]; // 清楚缓存数据
Expand All @@ -371,9 +380,14 @@ - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *

}

- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
NSLog(@"didReadPartialDataOfLength--->%lu",(unsigned long)partialLength);
}


//socket接受消息
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

if ([WKSDK shared].isDebug) {
NSLog(@"读取到消息-> %@",data);
}
Expand All @@ -382,6 +396,8 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
NSLog(@"------------解包消息数量--------> %lu",(unsigned long)data.count);
[self handlePacketData:data];
}];

[sock readDataWithTimeout:-1 tag:0];
}

// 发送连接包
Expand Down Expand Up @@ -584,7 +600,7 @@ -(NSMutableData*) unpackOneLM:(NSMutableData*)packData callback:(void(^) (NSData
int remLengthLength = pos - fixedHeaderLength; // 剩余长度的长度
if (fixedHeaderLength + remLengthLength + remLength > length) {
// 固定头的长度 + 剩余长度的长度 + 剩余长度 如果大于 总长度说明分包了
NSLog(@"分包了...");
NSLog(@"分包了...剩余长度:%d 当前长度:%lu",remLength,(unsigned long)length);
return packData;
}else {
if (fixedHeaderLength + remLengthLength + remLength == length) {
Expand All @@ -594,10 +610,16 @@ -(NSMutableData*) unpackOneLM:(NSMutableData*)packData callback:(void(^) (NSData
return [[NSMutableData alloc] init];
} else {
// 粘包 大于1个包
NSLog(@"粘包 大于1个包");
int packetLength = fixedHeaderLength + remLengthLength + remLength;;
callback([packData subdataWithRange:NSMakeRange(0, packetLength)]);
return [[NSMutableData alloc] initWithData:[packData subdataWithRange:NSMakeRange(packetLength, length-packetLength)]];
int packetLength = fixedHeaderLength + remLengthLength + remLength;

NSData *fullPackData = [packData subdataWithRange:NSMakeRange(0, packetLength)];

callback(fullPackData);
NSData *remPackData = [packData subdataWithRange:NSMakeRange(packetLength, length-packetLength)];

NSLog(@"粘包 大于1个包,包长度:%i-%lu-%lu-%lu",packetLength,fullPackData.length,remPackData.length,length);

return [[NSMutableData alloc] initWithData:remPackData];
}
}
}
Expand Down

0 comments on commit 4e26a8e

Please sign in to comment.