Authored by 盖剑秋

Add protection for socket reconnect.

... ... @@ -560,10 +560,10 @@
2E487B2F1D66B2FB009DBB6D /* YH_Log.h */,
2E487B301D66B2FB009DBB6D /* YH_Log.m */,
2E487AE71D66ADB8009DBB6D /* UtilsMacros.h */,
2EBBFF911D1911D20054E43F /* YH_SocketService.h */,
2E487ADC1D66ABBD009DBB6D /* YHL_Enums.h */,
31CCA5341D26111D00ADF9F0 /* DimensMacros.h */,
2E487ADD1D66ABD7009DBB6D /* YHL_ApiStringMacros.h */,
2EBBFF911D1911D20054E43F /* YH_SocketService.h */,
2EBBFF921D1911D20054E43F /* YH_SocketService.m */,
2EEAD4BA1D112D04003EE0E9 /* GCDAsyncSocket.h */,
2E487ADE1D66AC4C009DBB6D /* YH_NetworkAdapter+Live.h */,
... ...
... ... @@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, SOCKET_TAG) {
SOCKET_TAG_EndPlay = 11, //结束播放
SOCKET_TAG_NotifyEndPlay = 12, //服务器通知结束播放
SOCKET_TAG_NotifyOnlineAndLikes = 13, //服务器通知点赞数和在线人数
SOCKET_TAG_Start=14, //通知开始,重置弹幕服务器。
SOCKET_TAG_NotifyEndSuccess = 99, //服务器告知推流发起结束成功
};
... ...
... ... @@ -22,6 +22,7 @@
{
BOOL _hasLoginWithNameSuccessed;
BOOL _hasLoginWithDevieToken;
BOOL _needSendStartPackage;
}
@property (strong, nonatomic) NSTimer *connectTimer;
@property (strong, nonatomic) NSTimer *heartBeatTimer;
... ... @@ -37,6 +38,7 @@
{
self = [super init];
if (self) {
_needSendStartPackage = YES;
self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
self.deviceToken = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
... ... @@ -197,12 +199,14 @@
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
DLog(@"socket connect success!");
NSDictionary *params = @{@"cmd":@(14),
@"room":self.room?:@"666",
@"msg":@"",
@"uid":@""
};
[_socket writeDataWithParams:params tag:14];
if (_needSendStartPackage) {
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_Start),
@"room":self.room?:@"666",
@"msg":@"",
@"uid":@""
};
[_socket writeDataWithParams:params tag:SOCKET_TAG_Start];
}
self.heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES];
[self listenData];
}
... ... @@ -237,9 +241,10 @@
}
break;
case 14:
case SOCKET_TAG_Start:
{
[self loginToServer];
_needSendStartPackage = NO;
}
break;
default:
... ... @@ -350,6 +355,7 @@
[self.connectTimer invalidate];
self.connectTimer = nil;
}
_needSendStartPackage = YES;
[_socket disconnect];
}
... ...