Authored by 盖剑秋

Add protection for socket reconnect.

@@ -560,10 +560,10 @@ @@ -560,10 +560,10 @@
560 2E487B2F1D66B2FB009DBB6D /* YH_Log.h */, 560 2E487B2F1D66B2FB009DBB6D /* YH_Log.h */,
561 2E487B301D66B2FB009DBB6D /* YH_Log.m */, 561 2E487B301D66B2FB009DBB6D /* YH_Log.m */,
562 2E487AE71D66ADB8009DBB6D /* UtilsMacros.h */, 562 2E487AE71D66ADB8009DBB6D /* UtilsMacros.h */,
563 - 2EBBFF911D1911D20054E43F /* YH_SocketService.h */,  
564 2E487ADC1D66ABBD009DBB6D /* YHL_Enums.h */, 563 2E487ADC1D66ABBD009DBB6D /* YHL_Enums.h */,
565 31CCA5341D26111D00ADF9F0 /* DimensMacros.h */, 564 31CCA5341D26111D00ADF9F0 /* DimensMacros.h */,
566 2E487ADD1D66ABD7009DBB6D /* YHL_ApiStringMacros.h */, 565 2E487ADD1D66ABD7009DBB6D /* YHL_ApiStringMacros.h */,
  566 + 2EBBFF911D1911D20054E43F /* YH_SocketService.h */,
567 2EBBFF921D1911D20054E43F /* YH_SocketService.m */, 567 2EBBFF921D1911D20054E43F /* YH_SocketService.m */,
568 2EEAD4BA1D112D04003EE0E9 /* GCDAsyncSocket.h */, 568 2EEAD4BA1D112D04003EE0E9 /* GCDAsyncSocket.h */,
569 2E487ADE1D66AC4C009DBB6D /* YH_NetworkAdapter+Live.h */, 569 2E487ADE1D66AC4C009DBB6D /* YH_NetworkAdapter+Live.h */,
@@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, SOCKET_TAG) { @@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, SOCKET_TAG) {
36 SOCKET_TAG_EndPlay = 11, //结束播放 36 SOCKET_TAG_EndPlay = 11, //结束播放
37 SOCKET_TAG_NotifyEndPlay = 12, //服务器通知结束播放 37 SOCKET_TAG_NotifyEndPlay = 12, //服务器通知结束播放
38 SOCKET_TAG_NotifyOnlineAndLikes = 13, //服务器通知点赞数和在线人数 38 SOCKET_TAG_NotifyOnlineAndLikes = 13, //服务器通知点赞数和在线人数
  39 + SOCKET_TAG_Start=14, //通知开始,重置弹幕服务器。
39 SOCKET_TAG_NotifyEndSuccess = 99, //服务器告知推流发起结束成功 40 SOCKET_TAG_NotifyEndSuccess = 99, //服务器告知推流发起结束成功
40 }; 41 };
41 42
@@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
22 { 22 {
23 BOOL _hasLoginWithNameSuccessed; 23 BOOL _hasLoginWithNameSuccessed;
24 BOOL _hasLoginWithDevieToken; 24 BOOL _hasLoginWithDevieToken;
  25 + BOOL _needSendStartPackage;
25 } 26 }
26 @property (strong, nonatomic) NSTimer *connectTimer; 27 @property (strong, nonatomic) NSTimer *connectTimer;
27 @property (strong, nonatomic) NSTimer *heartBeatTimer; 28 @property (strong, nonatomic) NSTimer *heartBeatTimer;
@@ -37,6 +38,7 @@ @@ -37,6 +38,7 @@
37 { 38 {
38 self = [super init]; 39 self = [super init];
39 if (self) { 40 if (self) {
  41 + _needSendStartPackage = YES;
40 self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)]; 42 self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
41 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil]; 43 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
42 self.deviceToken = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]]; 44 self.deviceToken = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
@@ -197,12 +199,14 @@ @@ -197,12 +199,14 @@
197 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port 199 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
198 { 200 {
199 DLog(@"socket connect success!"); 201 DLog(@"socket connect success!");
200 - NSDictionary *params = @{@"cmd":@(14),  
201 - @"room":self.room?:@"666",  
202 - @"msg":@"",  
203 - @"uid":@""  
204 - };  
205 - [_socket writeDataWithParams:params tag:14]; 202 + if (_needSendStartPackage) {
  203 + NSDictionary *params = @{@"cmd":@(SOCKET_TAG_Start),
  204 + @"room":self.room?:@"666",
  205 + @"msg":@"",
  206 + @"uid":@""
  207 + };
  208 + [_socket writeDataWithParams:params tag:SOCKET_TAG_Start];
  209 + }
206 self.heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES]; 210 self.heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES];
207 [self listenData]; 211 [self listenData];
208 } 212 }
@@ -237,9 +241,10 @@ @@ -237,9 +241,10 @@
237 } 241 }
238 break; 242 break;
239 243
240 - case 14: 244 + case SOCKET_TAG_Start:
241 { 245 {
242 [self loginToServer]; 246 [self loginToServer];
  247 + _needSendStartPackage = NO;
243 } 248 }
244 break; 249 break;
245 default: 250 default:
@@ -350,6 +355,7 @@ @@ -350,6 +355,7 @@
350 [self.connectTimer invalidate]; 355 [self.connectTimer invalidate];
351 self.connectTimer = nil; 356 self.connectTimer = nil;
352 } 357 }
  358 + _needSendStartPackage = YES;
353 [_socket disconnect]; 359 [_socket disconnect];
354 } 360 }
355 361