|
@@ -9,17 +9,17 @@ |
|
@@ -9,17 +9,17 @@ |
9
|
#import "YH_SocketService.h"
|
9
|
#import "YH_SocketService.h"
|
10
|
#import "GCDAsyncSocket+SocketHandler.h"
|
10
|
#import "GCDAsyncSocket+SocketHandler.h"
|
11
|
|
11
|
|
12
|
-
|
|
|
13
|
#define TimeIntervalForHeartBeat 59
|
12
|
#define TimeIntervalForHeartBeat 59
|
14
|
#define SOCKET_COMMAND @"cmd"
|
13
|
#define SOCKET_COMMAND @"cmd"
|
15
|
#define SOCKET_CONNECT_MAX_COUNT 9
|
14
|
#define SOCKET_CONNECT_MAX_COUNT 9
|
16
|
|
15
|
|
17
|
@interface YH_SocketService ()<GCDAsyncSocketDelegate>
|
16
|
@interface YH_SocketService ()<GCDAsyncSocketDelegate>
|
18
|
{
|
17
|
{
|
19
|
- GCDAsyncSocket *_socket;
|
|
|
20
|
- NSTimer *_heartBeatTimer; // ❤️
|
18
|
+ BOOL _hasLoginWithNameSuccessed;
|
21
|
}
|
19
|
}
|
22
|
@property (nonatomic, strong) NSTimer *connectTimer;
|
20
|
@property (nonatomic, strong) NSTimer *connectTimer;
|
|
|
21
|
+@property (nonatomic, strong) NSTimer *heartBeatTimer; // ❤️
|
|
|
22
|
+@property (nonatomic, strong) GCDAsyncSocket *socket;
|
23
|
|
23
|
|
24
|
@end
|
24
|
@end
|
25
|
|
25
|
|
|
@@ -30,24 +30,69 @@ |
|
@@ -30,24 +30,69 @@ |
30
|
{
|
30
|
{
|
31
|
self = [super init];
|
31
|
self = [super init];
|
32
|
if (self) {
|
32
|
if (self) {
|
33
|
- _socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
34
|
- _heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES];
|
33
|
+ self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
34
|
+ self.heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:TimeIntervalForHeartBeat target:self selector:@selector(_heartbeat) userInfo:nil repeats:YES];
|
|
|
35
|
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
|
|
|
36
|
+
|
35
|
}
|
37
|
}
|
36
|
|
38
|
|
37
|
return self;
|
39
|
return self;
|
38
|
}
|
40
|
}
|
39
|
|
41
|
|
40
|
-- (void)reset
|
42
|
+- (NSString *)getProperIPWithAddress:(NSString *)ipAddr port:(UInt32)port
|
41
|
{
|
43
|
{
|
42
|
- DLog(@"socket service reset!!");
|
|
|
43
|
- if (_heartBeatTimer) {
|
|
|
44
|
- [_heartBeatTimer invalidate];
|
|
|
45
|
- _heartBeatTimer = nil;
|
44
|
+ NSError *addresseError = nil;
|
|
|
45
|
+ NSArray *addresseArray = [GCDAsyncSocket lookupHost:ipAddr
|
|
|
46
|
+ port:port
|
|
|
47
|
+ error:&addresseError];
|
|
|
48
|
+ if (addresseError) {
|
|
|
49
|
+ NSLog(@"");
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ NSString *ipv6Addr = @"";
|
|
|
53
|
+ for (NSData *addrData in addresseArray) {
|
|
|
54
|
+ if ([GCDAsyncSocket isIPv6Address:addrData]) {
|
|
|
55
|
+ ipv6Addr = [GCDAsyncSocket hostFromAddress:addrData];
|
|
|
56
|
+ }
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ if (ipv6Addr.length == 0) {
|
|
|
60
|
+ ipv6Addr = ipAddr;
|
46
|
}
|
61
|
}
|
47
|
|
62
|
|
|
|
63
|
+ return ipv6Addr;
|
|
|
64
|
+}
|
|
|
65
|
+
|
|
|
66
|
+//网络发生变化时重连
|
|
|
67
|
+- (void)networkReachabilityDidChange:(NSNotification *)notification
|
|
|
68
|
+{
|
|
|
69
|
+ [self reConnect];
|
|
|
70
|
+}
|
|
|
71
|
+
|
|
|
72
|
+- (void)reConnect
|
|
|
73
|
+{
|
48
|
[_socket disconnect];
|
74
|
[_socket disconnect];
|
|
|
75
|
+ self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
|
|
|
76
|
+ [self connectToHostRepeat];
|
49
|
}
|
77
|
}
|
50
|
|
78
|
|
|
|
79
|
+- (void)dealloc
|
|
|
80
|
+{
|
|
|
81
|
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
82
|
+}
|
|
|
83
|
+
|
|
|
84
|
+
|
|
|
85
|
+//- (void)reset
|
|
|
86
|
+//{
|
|
|
87
|
+// DLog(@"socket service reset!!");
|
|
|
88
|
+// if (_heartBeatTimer) {
|
|
|
89
|
+// [_heartBeatTimer invalidate];
|
|
|
90
|
+// _heartBeatTimer = nil;
|
|
|
91
|
+// }
|
|
|
92
|
+//
|
|
|
93
|
+// [_socket disconnect];
|
|
|
94
|
+//}
|
|
|
95
|
+
|
51
|
#pragma mark - Private Methods
|
96
|
#pragma mark - Private Methods
|
52
|
- (void)_heartbeat{
|
97
|
- (void)_heartbeat{
|
53
|
|
98
|
|
|
@@ -61,13 +106,17 @@ |
|
@@ -61,13 +106,17 @@ |
61
|
|
106
|
|
62
|
|
107
|
|
63
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_HEARTBEAT),
|
108
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_HEARTBEAT),
|
64
|
- @"msg":@"heartbeat",
|
109
|
+ @"msg":@"heart_ios",
|
65
|
@"room":MakeStringNotNil(self.room),
|
110
|
@"room":MakeStringNotNil(self.room),
|
66
|
};
|
111
|
};
|
67
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
112
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
68
|
|
113
|
|
69
|
- [_socket readDataWithTimeout:1.0 tag:1];
|
|
|
70
|
-
|
114
|
+// [self loginToServer];
|
|
|
115
|
+// [self listenData];
|
|
|
116
|
+ //心跳并判断是否已经断掉
|
|
|
117
|
+ if (![_socket isConnected]) {
|
|
|
118
|
+ [self reConnect];
|
|
|
119
|
+ }
|
71
|
}
|
120
|
}
|
72
|
|
121
|
|
73
|
#pragma mark - Service
|
122
|
#pragma mark - Service
|
|
@@ -81,7 +130,6 @@ |
|
@@ -81,7 +130,6 @@ |
81
|
[[NSRunLoop mainRunLoop] addTimer:self.connectTimer forMode:NSRunLoopCommonModes];
|
130
|
[[NSRunLoop mainRunLoop] addTimer:self.connectTimer forMode:NSRunLoopCommonModes];
|
82
|
} else {
|
131
|
} else {
|
83
|
NSLog(@"connent success!");
|
132
|
NSLog(@"connent success!");
|
84
|
- [self loginToServer];
|
|
|
85
|
}
|
133
|
}
|
86
|
} else {
|
134
|
} else {
|
87
|
NSLog(@"get addressList failed!");
|
135
|
NSLog(@"get addressList failed!");
|
|
@@ -133,11 +181,13 @@ |
|
@@ -133,11 +181,13 @@ |
133
|
|
181
|
|
134
|
- (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port
|
182
|
- (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port
|
135
|
{
|
183
|
{
|
|
|
184
|
+ NSString *hostProper = [self getProperIPWithAddress:host port:port];
|
|
|
185
|
+
|
136
|
NSError *socketConnectError = nil;
|
186
|
NSError *socketConnectError = nil;
|
137
|
|
187
|
|
138
|
NSAssert(_socket, @"You must init YH_SocketService first!");
|
188
|
NSAssert(_socket, @"You must init YH_SocketService first!");
|
139
|
|
189
|
|
140
|
- [_socket connectToHost:host onPort:port error:&socketConnectError];
|
190
|
+ [_socket connectToHost:hostProper onPort:port error:&socketConnectError];
|
141
|
|
191
|
|
142
|
if (socketConnectError) {
|
192
|
if (socketConnectError) {
|
143
|
DLog(@"connect host error: %@", [socketConnectError localizedDescription]);
|
193
|
DLog(@"connect host error: %@", [socketConnectError localizedDescription]);
|
|
@@ -159,6 +209,7 @@ |
|
@@ -159,6 +209,7 @@ |
159
|
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
|
209
|
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
|
160
|
{
|
210
|
{
|
161
|
DLog(@"socket connect success!");
|
211
|
DLog(@"socket connect success!");
|
|
|
212
|
+ [self loginToServer];
|
162
|
[self listenData];
|
213
|
[self listenData];
|
163
|
}
|
214
|
}
|
164
|
|
215
|
|
|
@@ -181,7 +232,7 @@ |
|
@@ -181,7 +232,7 @@ |
181
|
switch (tag) {
|
232
|
switch (tag) {
|
182
|
case SOCKET_TAG_LOGIN:
|
233
|
case SOCKET_TAG_LOGIN:
|
183
|
{
|
234
|
{
|
184
|
- [self _heartbeat];
|
235
|
+// [self _heartbeat];
|
185
|
}
|
236
|
}
|
186
|
break;
|
237
|
break;
|
187
|
|
238
|
|
|
@@ -225,6 +276,9 @@ |
|
@@ -225,6 +276,9 @@ |
225
|
case SOCKET_TAG_RESIVE_MSG:
|
276
|
case SOCKET_TAG_RESIVE_MSG:
|
226
|
case SOCKET_TAG_USER_JOIN:
|
277
|
case SOCKET_TAG_USER_JOIN:
|
227
|
{
|
278
|
{
|
|
|
279
|
+ if (resDic && [resDic.allKeys containsObject:@"uid"] && [resDic[@"uid"] isEqualToString:self.uid] && [self.uid notNilOrEmpty]) {
|
|
|
280
|
+ _hasLoginWithNameSuccessed = YES;
|
|
|
281
|
+ }
|
228
|
[self.barrageViewController insertItem:resDic];
|
282
|
[self.barrageViewController insertItem:resDic];
|
229
|
}
|
283
|
}
|
230
|
break;
|
284
|
break;
|
|
@@ -232,8 +286,8 @@ |
|
@@ -232,8 +286,8 @@ |
232
|
{
|
286
|
{
|
233
|
if ([self.delegate respondsToSelector:@selector(userPraised:isSelfPraise:)] && [resDic.allKeys containsObject:@"msg"]) {
|
287
|
if ([self.delegate respondsToSelector:@selector(userPraised:isSelfPraise:)] && [resDic.allKeys containsObject:@"msg"]) {
|
234
|
yh_dispatch_execute_in_main_queue(^{
|
288
|
yh_dispatch_execute_in_main_queue(^{
|
235
|
-
|
|
|
236
|
- [self.delegate userPraised:resDic[@"msg"] isSelfPraise:([resDic[@"uid"] isEqualToString:self.uid])];
|
289
|
+// BOOL isSelfPraise =(resDic[@"deviceToken"] && [resDic[@"deviceToken"] isEqualToString:[GVUserDefaults standardUserDefaults].deviceTokenString]);
|
|
|
290
|
+ [self.delegate userPraised:resDic[@"msg"] isSelfPraise:NO];
|
237
|
});
|
291
|
});
|
238
|
}
|
292
|
}
|
239
|
}
|
293
|
}
|
|
@@ -242,7 +296,7 @@ |
|
@@ -242,7 +296,7 @@ |
242
|
{
|
296
|
{
|
243
|
if ([self.delegate respondsToSelector:@selector(currentPeopleNumber:)] && [resDic.allKeys containsObject:@"msg"]) {
|
297
|
if ([self.delegate respondsToSelector:@selector(currentPeopleNumber:)] && [resDic.allKeys containsObject:@"msg"]) {
|
244
|
yh_dispatch_execute_in_main_queue(^{
|
298
|
yh_dispatch_execute_in_main_queue(^{
|
245
|
- [self.delegate currentPeopleNumber:resDic[@"msg"]];
|
299
|
+ [self.delegate currentPeopleNumber:resDic[@"onlineNums"]];
|
246
|
});
|
300
|
});
|
247
|
}
|
301
|
}
|
248
|
}
|
302
|
}
|
|
@@ -256,6 +310,15 @@ |
|
@@ -256,6 +310,15 @@ |
256
|
}
|
310
|
}
|
257
|
}
|
311
|
}
|
258
|
break;
|
312
|
break;
|
|
|
313
|
+ case SOCKET_TAG_NotifyOnlineAndLikes:
|
|
|
314
|
+ {
|
|
|
315
|
+ if ([self.delegate respondsToSelector:@selector(liveOnlineNums:likes:)]) {
|
|
|
316
|
+ yh_dispatch_execute_in_main_queue(^{
|
|
|
317
|
+ [self.delegate liveOnlineNums:resDic[@"onlineNums"] likes:resDic[@"likeNums"]];
|
|
|
318
|
+ });
|
|
|
319
|
+ }
|
|
|
320
|
+ }
|
|
|
321
|
+ break;
|
259
|
default:
|
322
|
default:
|
260
|
break;
|
323
|
break;
|
261
|
}
|
324
|
}
|
|
@@ -288,10 +351,10 @@ |
|
@@ -288,10 +351,10 @@ |
288
|
- (void)loginToServer
|
351
|
- (void)loginToServer
|
289
|
{
|
352
|
{
|
290
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_LOGIN),
|
353
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_LOGIN),
|
291
|
- @"uid":MakeStringNotNil(self.uid),
|
354
|
+ @"uid":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.uid)),
|
292
|
@"room":MakeStringNotNil(self.room),
|
355
|
@"room":MakeStringNotNil(self.room),
|
293
|
- @"name":MakeStringNotNil(self.userName),
|
|
|
294
|
- @"avatar":MakeStringNotNil(self.avartar)
|
356
|
+ @"name":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.userName)),
|
|
|
357
|
+ @"avatar":(_hasLoginWithNameSuccessed?@"":MakeStringNotNil(self.avartar)),
|
295
|
};
|
358
|
};
|
296
|
|
359
|
|
297
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_LOGIN];
|
360
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_LOGIN];
|
|
@@ -317,7 +380,8 @@ |
|
@@ -317,7 +380,8 @@ |
317
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_PRAISE),
|
380
|
NSDictionary *params = @{@"cmd":@(SOCKET_TAG_PRAISE),
|
318
|
@"uid":MakeStringNotNil(self.uid),
|
381
|
@"uid":MakeStringNotNil(self.uid),
|
319
|
@"msg":@"",
|
382
|
@"msg":@"",
|
320
|
- @"room":MakeStringNotNil(self.room)
|
383
|
+ @"room":MakeStringNotNil(self.room),
|
|
|
384
|
+ @"deviceToken":@"",
|
321
|
};
|
385
|
};
|
322
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
386
|
[_socket writeDataWithParams:params tag:SOCKET_TAG_SEND_MSG];
|
323
|
}
|
387
|
}
|