Skip to content

Commit 404d5a9

Browse files
author
mengyaoyao
committed
自动重连 , 建立连接 , 主动断开连接相关
1 parent 66c3b0b commit 404d5a9

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

CocoaAsyncSocket_TCP/ChatHandler/ChatHandler.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ - (void)disconnect
156156
[_chatSocket disconnect];
157157
//关闭心跳定时器
158158
dispatch_source_cancel(self.beatTimer);
159-
//置为初始化
159+
//未接收到服务器心跳次数,置为初始化
160160
_senBeatCount = 0;
161+
//自动重连次数 , 置为初始化
162+
autoConnectCount = TCP_AutoConnectCount;
161163
}
162164

163165

@@ -192,7 +194,7 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
192194
//接收到服务器的心跳
193195
if ([messageModel.beatID isEqualToString:TCP_beatBody]) {
194196

195-
//置为0
197+
//未接到服务器心跳次数置为0
196198
_senBeatCount = 0;
197199
NSLog(@"------------------接收到服务器心跳-------------------");
198200
return;
@@ -222,6 +224,8 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
222224
messageType = ChatMessageType_LoginReceipt;
223225
//开始发送心跳
224226
[self sendBeat];
227+
//重新建立连接后 , 重置自动重连次数
228+
autoConnectCount = TCP_AutoConnectCount;
225229

226230
//发送普通消息失败回执
227231
}else if ([messageModel.messageType isEqualToString:Message_InvalidReceipt]){

CocoaAsyncSocket_TCP/Controller/ChatListViewController.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,14 @@ - (void)configNav_Badges
261261
全局咱们设定了一个ChatHandler单例,用于处理TCP的相关逻辑 。那么当TCP推送过来消息时,我该将这些消息发给谁?谁注册成为我的代理,我就发给谁。
262262
ChatHandler单例为全局的,并且生命周期为整个app运行期间不会销毁。在ChatHandler中引用一个数组 ,该数组中存放所有注册成为需要收取消息的代理,当每来一条消息时,遍历该数组,并向所有的代理推送该条消息.
263263
264+
<<<<Socket建立连接时机>>>>
265+
什么时候开始建立socket连接 ? 1 . 应用启动时 , 建立socket服务器连接 2 . 登录成功时 , 建立socket服务器连接
264266
267+
<<<<Socket断开时机>>>>
268+
什么时候开始主动断开socket连接 ? 1 . 当用户退出当前账号时 2. 当检测到网络断开时
265269
270+
<<<<关于重连机制>>>>
271+
什么时候触发重连 ? 1 . 当3次没有收到服务器的心跳时 , 默认进行重连 . 2 . 当检测到socket断开连接时 , 默认进行3次重连 3 . 当网络状态良好,但socket处于断开状态时,进行重连
266272
267273
268274
<<<<聊天UI的搭建>>>>

CocoaAsyncSocket_TCP/Controller/LastViewController.m

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ - (void)viewDidLoad {
1919
// Do any additional setup after loading the view.
2020
}
2121

22-
- (void)didReceiveMemoryWarning {
23-
[super didReceiveMemoryWarning];
24-
// Dispose of any resources that can be recreated.
25-
}
26-
27-
/*
28-
#pragma mark - Navigation
2922

30-
// In a storyboard-based application, you will often want to do a little preparation before navigation
31-
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
32-
// Get the new view controller using [segue destinationViewController].
33-
// Pass the selected object to the new view controller.
34-
}
35-
*/
3623

3724
@end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy