...
|
...
|
@@ -9,17 +9,17 @@ |
|
|
#import "YH_SocketService.h"
|
|
|
#import "GCDAsyncSocket+SocketHandler.h"
|
|
|
|
|
|
|
|
|
#define TimeIntervalForHeartBeat 59
|
|
|
#define SOCKET_COMMAND @"cmd"
|
|
|
#define SOCKET_CONNECT_MAX_COUNT 9
|
|
|
|
|
|
@interface YH_SocketService ()<GCDAsyncSocketDelegate>
|
|
|
{
|
|
|
GCDAsyncSocket *_socket;
|
|
|
NSTimer *_heartBeatTimer; // ❤️
|
|
|
BOOL _hasLoginWithNameSuccessed;
|
|
|
}
|
|
|
@property (nonatomic, strong) NSTimer *connectTimer;
|
|
|
@property (nonatomic, strong) NSTimer *heartBeatTimer; // ❤️
|
|
|
@property (nonatomic, strong) GCDAsyncSocket *socket;
|
|
|
|
|
|
@end
|
|
|
|
...
|
...
|
@@ -30,24 +30,69 @@ |
|
|
{
|
|
|
self = [super init];
|
|
|
if (self) {
|
|
|
_socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
_heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES];
|
|
|
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];
|
|
|
|
|
|
}
|
|
|
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
- (void)reset
|
|
|
- (NSString *)getProperIPWithAddress:(NSString *)ipAddr port:(UInt32)port
|
|
|
{
|
|
|
DLog(@"socket service reset!!");
|
|
|
if (_heartBeatTimer) {
|
|
|
[_heartBeatTimer invalidate];
|
|
|
_heartBeatTimer = nil;
|
|
|
NSError *addresseError = nil;
|
|
|
NSArray *addresseArray = [GCDAsyncSocket lookupHost:ipAddr
|
|
|
port:port
|
|
|
error:&addresseError];
|
|
|
if (addresseError) {
|
|
|
NSLog(@"");
|
|
|
}
|
|
|
|
|
|
NSString *ipv6Addr = @"";
|
|
|
for (NSData *addrData in addresseArray) {
|
|
|
if ([GCDAsyncSocket isIPv6Address:addrData]) {
|
|
|
ipv6Addr = [GCDAsyncSocket hostFromAddress:addrData];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (ipv6Addr.length == 0) {
|
|
|
ipv6Addr = ipAddr;
|
|
|
}
|
|
|
|
|
|
return ipv6Addr;
|
|
|
}
|
|
|
|
|
|
//网络发生变化时重连
|
|
|
- (void)networkReachabilityDidChange:(NSNotification *)notification
|
|
|
{
|
|
|
[self reConnect];
|
|
|
}
|
|
|
|
|
|
- (void)reConnect
|
|
|
{
|
|
|
[_socket disconnect];
|
|
|
self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
[self connectToHostRepeat];
|
|
|
}
|
|
|
|
|
|
- (void)dealloc
|
|
|
{
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
}
|
|
|
|
|
|
|
|
|
//- (void)reset
|
|
|
//{
|
|
|
// DLog(@"socket service reset!!");
|
|
|
// if (_heartBeatTimer) {
|
|
|
// [_heartBeatTimer invalidate];
|
|
|
// _heartBeatTimer = nil;
|
|
|
// }
|
|
|
//
|
|
|
// [_socket disconnect];
|
|
|
//}
|
|
|
|
|
|
#pragma mark - Private Methods
|
|
|
- (void)_heartbeat{
|
|
|
|
...
|
...
|
@@ -61,13 +106,17 @@ |
|
|
|
|
|
|
|
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_HEARTBEAT),
|
|
|
@"msg":@"heartbeat",
|
|
|
@"msg":@"heart_ios",
|
|
|
@"room":MakeStringNotNil(self.room),
|
|
|
};
|
|
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
|
|
|
|
|
[_socket readDataWithTimeout:1.0 tag:1];
|
|
|
|
|
|
// [self loginToServer];
|
|
|
// [self listenData];
|
|
|
//心跳并判断是否已经断掉
|
|
|
if (![_socket isConnected]) {
|
|
|
[self reConnect];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#pragma mark - Service
|
...
|
...
|
@@ -81,7 +130,6 @@ |
|
|
[[NSRunLoop mainRunLoop] addTimer:self.connectTimer forMode:NSRunLoopCommonModes];
|
|
|
} else {
|
|
|
NSLog(@"connent success!");
|
|
|
[self loginToServer];
|
|
|
}
|
|
|
} else {
|
|
|
NSLog(@"get addressList failed!");
|
...
|
...
|
@@ -133,11 +181,13 @@ |
|
|
|
|
|
- (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port
|
|
|
{
|
|
|
NSString *hostProper = [self getProperIPWithAddress:host port:port];
|
|
|
|
|
|
NSError *socketConnectError = nil;
|
|
|
|
|
|
NSAssert(_socket, @"You must init YH_SocketService first!");
|
|
|
|
|
|
[_socket connectToHost:host onPort:port error:&socketConnectError];
|
|
|
[_socket connectToHost:hostProper onPort:port error:&socketConnectError];
|
|
|
|
|
|
if (socketConnectError) {
|
|
|
DLog(@"connect host error: %@", [socketConnectError localizedDescription]);
|
...
|
...
|
@@ -159,6 +209,7 @@ |
|
|
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
|
|
|
{
|
|
|
DLog(@"socket connect success!");
|
|
|
[self loginToServer];
|
|
|
[self listenData];
|
|
|
}
|
|
|
|
...
|
...
|
@@ -181,7 +232,7 @@ |
|
|
switch (tag) {
|
|
|
case SOCKET_TAG_LOGIN:
|
|
|
{
|
|
|
[self _heartbeat];
|
|
|
// [self _heartbeat];
|
|
|
}
|
|
|
break;
|
|
|
|
...
|
...
|
@@ -225,6 +276,9 @@ |
|
|
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;
|
|
|
}
|
|
|
[self.barrageViewController insertItem:resDic];
|
|
|
}
|
|
|
break;
|
...
|
...
|
@@ -232,8 +286,8 @@ |
|
|
{
|
|
|
if ([self.delegate respondsToSelector:@selector(userPraised:isSelfPraise:)] && [resDic.allKeys containsObject:@"msg"]) {
|
|
|
yh_dispatch_execute_in_main_queue(^{
|
|
|
|
|
|
[self.delegate userPraised:resDic[@"msg"] isSelfPraise:([resDic[@"uid"] isEqualToString:self.uid])];
|
|
|
// BOOL isSelfPraise =(resDic[@"deviceToken"] && [resDic[@"deviceToken"] isEqualToString:[GVUserDefaults standardUserDefaults].deviceTokenString]);
|
|
|
[self.delegate userPraised:resDic[@"msg"] isSelfPraise:NO];
|
|
|
});
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -242,7 +296,7 @@ |
|
|
{
|
|
|
if ([self.delegate respondsToSelector:@selector(currentPeopleNumber:)] && [resDic.allKeys containsObject:@"msg"]) {
|
|
|
yh_dispatch_execute_in_main_queue(^{
|
|
|
[self.delegate currentPeopleNumber:resDic[@"msg"]];
|
|
|
[self.delegate currentPeopleNumber:resDic[@"onlineNums"]];
|
|
|
});
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -256,6 +310,15 @@ |
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
case SOCKET_TAG_NotifyOnlineAndLikes:
|
|
|
{
|
|
|
if ([self.delegate respondsToSelector:@selector(liveOnlineNums:likes:)]) {
|
|
|
yh_dispatch_execute_in_main_queue(^{
|
|
|
[self.delegate liveOnlineNums:resDic[@"onlineNums"] likes:resDic[@"likeNums"]];
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
...
|
...
|
@@ -288,10 +351,10 @@ |
|
|
- (void)loginToServer
|
|
|
{
|
|
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_LOGIN),
|
|
|
@"uid":MakeStringNotNil(self.uid),
|
|
|
@"uid":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.uid)),
|
|
|
@"room":MakeStringNotNil(self.room),
|
|
|
@"name":MakeStringNotNil(self.userName),
|
|
|
@"avatar":MakeStringNotNil(self.avartar)
|
|
|
@"name":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.userName)),
|
|
|
@"avatar":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.avartar)),
|
|
|
};
|
|
|
|
|
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_LOGIN];
|
...
|
...
|
@@ -317,7 +380,8 @@ |
|
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_PRAISE),
|
|
|
@"uid":MakeStringNotNil(self.uid),
|
|
|
@"msg":@"",
|
|
|
@"room":MakeStringNotNil(self.room)
|
|
|
@"room":MakeStringNotNil(self.room),
|
|
|
@"deviceToken":@"",
|
|
|
};
|
|
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
|
|
}
|
...
|
...
|
|