.h文件
1
2
3
4
5
6
7
8
9
10
11
|
#import < Foundation /Foundation.h> #import "GCDAsyncSocket.h" @interface GCDSocketManager : NSObject @property(nonatomic,strong) GCDAsyncSocket *socket; //单例 + (instancetype)sharedSocketManager; //连接 - (void)connectToServer; //断开 - (void)cutOffSocket; @end |
.m文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#import "GCDSocketManager.h" #define SocketHost @"地址" #define SocketPort 端口 @interface GCDSocketManager()< GCDAsyncSocketDelegate > //握手次数 @property(nonatomic,assign) NSInteger pushCount; //断开重连定时器 @property(nonatomic,strong) NSTimer *timer; //重连次数 @property(nonatomic,assign) NSInteger reconnectCount; @end @implementation GCDSocketManager //全局访问点 + (instancetype)sharedSocketManager { static GCDSocketManager *_instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } //可以在这里做一些初始化操作 - (instancetype)init { self = [super init]; if (self) { } return self; } #pragma mark 请求连接 //连接 - (void)connectToServer { self.pushCount = 0; self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; [self.socket connectToHost:SocketHost onPort:SocketPort error:&error]; if (error) { DLog(@"SocketConnectError:%@",error); } } #pragma mark 连接成功 //连接成功的回调 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { DLog(@"socket连接成功"); [self sendDataToServer]; } //连接成功后向服务器发送数据 - (void)sendDataToServer { //发送数据代码省略... //发送 [self.socket writeData:jsonData withTimeout:-1 tag:1]; //读取数据 [self.socket readDataWithTimeout:-1 tag:200]; } //连接成功向服务器发送数据后,服务器会有响应 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { [self.socket readDataWithTimeout:-1 tag:200]; //服务器推送次数 self.pushCount++; //在这里进行校验操作,情况分为成功和失败两种,成功的操作一般都是拉取数据 } #pragma mark 连接失败 //连接失败的回调 - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { DLog(@"Socket连接失败"); self.pushCount = 0; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *currentStatu = [userDefaults valueForKey:@"Statu"]; //程序在前台才进行重连 if ([currentStatu isEqualToString:@"foreground"]) { //重连次数 self.reconnectCount++; //如果连接失败 累加1秒重新连接 减少服务器压力 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 * self.reconnectCount target:self selector:@selector(reconnectServer) userInfo:nil repeats:NO]; self.timer = timer; } } //如果连接失败,5秒后重新连接 - (void)reconnectServer { self.pushCount = 0; self.reconnectCount = 0; //连接失败重新连接 NSError *error = nil; [self.socket connectToHost:SocketHost onPort:SocketPort error:&error]; if (error) { DLog(@"SocektConnectError:%@",error); } } #pragma mark 断开连接 //切断连接 - (void)cutOffSocket { DLog(@"socket断开连接"); self.pushCount = 0; self.reconnectCount = 0; [self.timer invalidate]; self.timer = nil; [self.socket disconnect]; } @end |
以上这篇iOS使用GCDSocketManager实现长连接的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/60fd4ae4cd74