...
|
...
|
@@ -16,10 +16,12 @@ |
|
|
@interface YH_SocketService ()<GCDAsyncSocketDelegate>
|
|
|
{
|
|
|
BOOL _hasLoginWithNameSuccessed;
|
|
|
BOOL _hasLoginWithDevieToken;
|
|
|
}
|
|
|
@property (nonatomic, strong) NSTimer *connectTimer;
|
|
|
@property (nonatomic, strong) NSTimer *heartBeatTimer; // ❤️
|
|
|
@property (nonatomic, strong) GCDAsyncSocket *socket;
|
|
|
@property (nonatomic, strong) NSString *deviceToken;
|
|
|
|
|
|
@end
|
|
|
|
...
|
...
|
@@ -33,7 +35,7 @@ |
|
|
self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
self.heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES];
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
|
|
|
|
|
|
self.deviceToken = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
|
|
|
}
|
|
|
|
|
|
return self;
|
...
|
...
|
@@ -72,6 +74,7 @@ |
|
|
- (void)reConnect
|
|
|
{
|
|
|
[_socket disconnect];
|
|
|
_hasLoginWithDevieToken = NO;
|
|
|
self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
[self connectToHostRepeat];
|
|
|
}
|
...
|
...
|
@@ -117,6 +120,10 @@ |
|
|
if (![_socket isConnected]) {
|
|
|
[self reConnect];
|
|
|
}
|
|
|
//判断上次是否登录成功
|
|
|
if (!_hasLoginWithDevieToken) {
|
|
|
[self loginToServer];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#pragma mark - Service
|
...
|
...
|
@@ -273,12 +280,17 @@ |
|
|
if (resDic && [resDic.allKeys containsObject:SOCKET_COMMAND]) {
|
|
|
int cmd = [[resDic objectForKey:SOCKET_COMMAND] intValue];
|
|
|
switch (cmd) {
|
|
|
case SOCKET_TAG_RESIVE_MSG:
|
|
|
case SOCKET_TAG_USER_JOIN:
|
|
|
{
|
|
|
if (resDic && [resDic.allKeys containsObject:@"uid"] && [resDic[@"uid"] isEqualToString:self.uid] && [self.uid notNilOrEmpty]) {
|
|
|
_hasLoginWithNameSuccessed = YES;
|
|
|
}
|
|
|
if (resDic && [resDic.allKeys containsObject:@"deviceToken"] && [resDic[@"deviceToken"] isEqualToString:self.deviceToken] && [self.deviceToken notNilOrEmpty]) {
|
|
|
_hasLoginWithDevieToken = YES;
|
|
|
}
|
|
|
}
|
|
|
case SOCKET_TAG_RESIVE_MSG:
|
|
|
{
|
|
|
[self.barrageViewController insertItem:resDic];
|
|
|
}
|
|
|
break;
|
...
|
...
|
@@ -286,7 +298,7 @@ |
|
|
{
|
|
|
if ([self.delegate respondsToSelector:@selector(userPraised:isSelfPraise:)] && [resDic.allKeys containsObject:@"msg"]) {
|
|
|
yh_dispatch_execute_in_main_queue(^{
|
|
|
// BOOL isSelfPraise =(resDic[@"deviceToken"] && [resDic[@"deviceToken"] isEqualToString:[GVUserDefaults standardUserDefaults].deviceTokenString]);
|
|
|
// BOOL isSelfPraise =(resDic[@"deviceToken"] && [resDic[@"deviceToken"] isEqualToString:self.deviceToken]);
|
|
|
[self.delegate userPraised:resDic[@"msg"] isSelfPraise:NO];
|
|
|
});
|
|
|
}
|
...
|
...
|
@@ -365,6 +377,7 @@ |
|
|
@"room":MakeStringNotNil(self.room),
|
|
|
@"name":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.userName)),
|
|
|
@"avatar":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.avartar)),
|
|
|
@"deviceToken":self.deviceToken,
|
|
|
};
|
|
|
|
|
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_LOGIN];
|
...
|
...
|
@@ -391,7 +404,7 @@ |
|
|
@"uid":MakeStringNotNil(self.uid),
|
|
|
@"msg":@"",
|
|
|
@"room":MakeStringNotNil(self.room),
|
|
|
@"deviceToken":@"",
|
|
|
@"deviceToken":self.deviceToken,
|
|
|
};
|
|
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
|
|
}
|
...
|
...
|
|