123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- //
- // WebSocketManager.m
- // Anchor
- //
- // Created by 翟玉磊 on 2019/3/26.
- // Copyright © 2019 翟玉磊. All rights reserved.
- //
- #import "WebSocketManager.h"
- #import "ZYLURLRequestGenerator.h"
- #import "LFCGzipUtility.h"
- @interface WebSocketManager()<SRWebSocketDelegate>
- @property (nonatomic, strong) NSTimer *heartBeatTimer; //心跳定时器
- @property (nonatomic, strong) NSTimer *netWorkTestingTimer; //没有网络的时候检测网络定时器
- @property (nonatomic, strong) dispatch_queue_t queue; //数据请求队列(串行队列)
- @property (nonatomic, assign) NSTimeInterval reConnectTime; //重连时间
- @property (nonatomic, strong) NSMutableArray *sendDataArray; //存储要发送给服务端的数据
- @property (nonatomic, assign) BOOL isActivelyClose; //用于判断是否主动关闭长连接,如果是主动断开连接,连接失败的代理中,就不用执行 重新连接方法
- /// 保存连接url
- @property (nonatomic, readwrite, copy) NSString *connectUrl;
- @property (nonatomic, readwrite, strong) IMConnectCompletionHandler connectCompletionHandler;
- @property (nonatomic, readwrite, strong) IMCloseCompletionHandler closeCompletionHandler;
- @end
- @implementation WebSocketManager
- //单例
- + (instancetype)sharedSocketManager
- {
- static WebSocketManager *_instace = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instace = [[self alloc] init];
- });
- return _instace;
- }
- - (instancetype)init
- {
- self = [super init];
- if(self)
- {
- self.reConnectTime = 0;
- self.isActivelyClose = NO;
- self.queue = dispatch_queue_create("BF",NULL);
- self.sendDataArray = [[NSMutableArray alloc] init];
- }
- return self;
- }
- #pragma mark - NSTimer
- //初始化心跳
- - (void)initHeartBeat
- {
- //心跳没有被关闭
- if(self.heartBeatTimer)
- {
- return;
- }
-
- [self destoryHeartBeat];
-
- WeakSelf
- dispatch_main_async_safe(^{
- // dispatch_async(dispatch_get_main_queue(), ^{
- weakSelf.heartBeatTimer = [NSTimer timerWithTimeInterval:20 target:weakSelf selector:@selector(senderheartBeat) userInfo:nil repeats:true];
- [[NSRunLoop currentRunLoop]addTimer:weakSelf.heartBeatTimer forMode:NSRunLoopCommonModes];
- });
- [self senderheartBeat];
- }
- //取消心跳
- - (void)destoryHeartBeat
- {
- WeakSelf
- dispatch_main_async_safe(^{
- // dispatch_async(dispatch_get_main_queue(), ^{
- if(weakSelf.heartBeatTimer)
- {
- NSLog(@"取消心跳");
- [weakSelf.heartBeatTimer invalidate];
- weakSelf.heartBeatTimer = nil;
- }
- });
- }
- //没有网络的时候开始定时 -- 用于网络检测
- - (void)noNetWorkStartTestingTimer
- {
- WeakSelf
- dispatch_main_async_safe(^{
- // dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"取消网络监测");
- weakSelf.netWorkTestingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakSelf selector:@selector(noNetWorkStartTesting) userInfo:nil repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:weakSelf.netWorkTestingTimer forMode:NSDefaultRunLoopMode];
- });
- }
- //取消网络检测
- - (void)destoryNetWorkStartTesting
- {
- WeakSelf
- dispatch_main_async_safe(^{
- //dispatch_async(dispatch_get_main_queue(), ^{
- if(weakSelf.netWorkTestingTimer)
- {
- [weakSelf.netWorkTestingTimer invalidate];
- weakSelf.netWorkTestingTimer = nil;
- }
- });
- }
- #pragma mark - private -- webSocket相关方法
- //发送心跳
- - (void)senderheartBeat
- {
- //和服务端约定好发送什么作为心跳标识,尽可能的减小心跳包大小
- WeakSelf
- dispatch_main_async_safe(^{
- if(weakSelf.webSocket.readyState == SR_OPEN)
- {
- NSString *text = @"ping";
- NSData * gZipData = [LFCGzipUtility gzipData:[text dataUsingEncoding:NSUTF8StringEncoding]];
- [weakSelf.webSocket send:gZipData];
- NSLog(@"发送ping:%@",text);
- }
- });
- }
- //定时检测网络
- - (void)noNetWorkStartTesting
- {
- //有网络
- if(AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus != AFNetworkReachabilityStatusNotReachable)
- {
- //关闭网络检测定时器
- [self destoryNetWorkStartTesting];
- //开始重连
- [self reConnectServer];
- }
- }
- //建立长连接
- - (void)connectServerWithUrl:(NSString *)url connectCompletionHandler:(nonnull IMConnectCompletionHandler)completionHandler
- {
- if (StringIsEmpty(_connectUrl)) {
- _connectUrl = url;
- }
- self.isActivelyClose = NO;
-
- if(_webSocket)
- {
- _webSocket = nil;
- }
-
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
- [[ZYLURLRequestGenerator sharedInstance] setCommonRequestHeaderForRequest:request];
- self.webSocket = [[SRWebSocket alloc] initWithURLRequest:request];
- self.webSocket.delegate = self;
- [self.webSocket open];
- NSLog(@"建立连接");
- self.connectCompletionHandler = completionHandler;
- }
- //重新连接服务器
- - (void)reConnectServer
- {
- if(self.webSocket.readyState == SR_OPEN)
- {
- return;
- }
-
- if(self.reConnectTime > 1024) //重连10次 2^10 = 1024
- {
- self.reConnectTime = 0;
- return;
- }
-
- WeakSelf
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.reConnectTime *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-
- if(weakSelf.webSocket.readyState == SR_OPEN && weakSelf.webSocket.readyState == SR_CONNECTING)
- {
- return;
- }
-
- [weakSelf connectServerWithUrl:self.connectUrl connectCompletionHandler:self.connectCompletionHandler];
- NSLog(@"正在重连......");
-
- if(weakSelf.reConnectTime == 0) //重连时间2的指数级增长
- {
- weakSelf.reConnectTime = 2;
- }
- else
- {
- weakSelf.reConnectTime *= 2;
- }
- });
-
- }
- //关闭连接
- - (void)SRWebSocketCloseCompletionHandler:(IMCloseCompletionHandler)completionHandler
- {
-
- self.isActivelyClose = YES;
- self.closeCompletionHandler = completionHandler;
-
- [self webSocketClose];
-
- //关闭心跳定时器
- [self destoryHeartBeat];
-
- //关闭网络检测定时器
- [self destoryNetWorkStartTesting];
- }
- //关闭连接
- - (void)webSocketClose
- {
- if(self.webSocket)
- {
- [self.webSocket close];
- NSLog(@"close:%ld",self.webSocket.readyState);
- _webSocket = nil;
- }
- }
- //发送数据给服务器
- - (void)sendDataToServer:(id)data
- {
- NSData * gZipData = [LFCGzipUtility gzipData:data];
- [self.sendDataArray addObject:gZipData];
- [self sendeDataToServer];
- }
- - (void)sendeDataToServer
- {
- WeakSelf
-
- //把数据放到一个请求队列中
- dispatch_async(self.queue, ^{
-
- //没有网络
- if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable)
- {
- //开启网络检测定时器
- [weakSelf noNetWorkStartTestingTimer];
- }
- else //有网络
- {
- if(weakSelf.webSocket != nil)
- {
- // 只有长连接OPEN开启状态才能调 send 方法,不然会Crash
- if(weakSelf.webSocket.readyState == SR_OPEN)
- {
- if (weakSelf.sendDataArray.count > 0)
- {
- NSString *data = weakSelf.sendDataArray[0];
- [weakSelf.webSocket send:data]; //发送数据
- [weakSelf.sendDataArray removeObjectAtIndex:0];
-
- if([weakSelf.sendDataArray count] > 0)
- {
- [weakSelf sendeDataToServer];
- }
- }
- }
- else if (weakSelf.webSocket.readyState == SR_CONNECTING) //正在连接
- {
- NSLog(@"正在连接中,重连后会去自动同步数据");
- }
- else if (weakSelf.webSocket.readyState == SR_CLOSING || weakSelf.webSocket.readyState == SR_CLOSED) //断开连接
- {
- //调用 reConnectServer 方法重连,连接成功后 继续发送数据
- [weakSelf reConnectServer];
- }
- }
- else
- {
- [weakSelf connectServerWithUrl:self.connectUrl connectCompletionHandler:self.connectCompletionHandler]; //连接服务器
- }
- }
- });
- }
- #pragma mark - SRWebSocketDelegate -- webSockect代理
- //连接成功回调
- - (void)webSocketDidOpen:(SRWebSocket *)webSocket
- {
- NSLog(@"webSocket === 连接成功");
-
- if (self.connectCompletionHandler) {
- self.connectCompletionHandler(0, @"连接成功");
- self.connectCompletionHandler = nil;
- }
-
- [self initHeartBeat]; //开启心跳
-
- //如果有尚未发送的数据,继续向服务端发送数据
- if ([self.sendDataArray count] > 0){
- [self sendeDataToServer];
- }
- }
- //连接失败回调
- - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
- {
- NSLog(@"webscoket连接失败回调:%ld",webSocket.readyState);
- //用户主动断开连接,就不去进行重连
- if(self.isActivelyClose)
- {
- if (self.closeCompletionHandler) {
- self.closeCompletionHandler(0, @"离开聊天队列成功");
- self.closeCompletionHandler = nil;
- }
- return;
- }
-
- [self destoryHeartBeat]; //断开连接时销毁心跳
-
- NSLog(@"连接失败,这里可以实现掉线自动重连,要注意以下几点");
- NSLog(@"1.判断当前网络环境,如果断网了就不要连了,等待网络到来,在发起重连");
- NSLog(@"3.连接次数限制,如果连接失败了,重试10次左右就可以了");
-
- //判断网络环境
- if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) //没有网络
- {
- [self noNetWorkStartTestingTimer];//开启网络检测定时器
- }
- else //有网络
- {
- [self reConnectServer];//连接失败就重连
- }
- }
- //连接关闭,注意连接关闭不是连接断开,关闭是 [socket close] 客户端主动关闭,断开可能是断网了,被动断开的。
- - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean
- {
- NSLog(@"webscoket连接关闭回调:%ld",webSocket.readyState);
-
- // 在这里判断 webSocket 的状态 是否为 open , 大家估计会有些奇怪 ,因为我们的服务器都在海外,会有些时间差,经过测试,我们在进行某次连接的时候,上次重连的回调刚好回来,而本次重连又成功了,就会误以为,本次没有重连成功,而再次进行重连,就会出现问题,所以在这里做了一下判断
- if(self.webSocket.readyState == SR_OPEN || self.isActivelyClose)
- {
- if (self.closeCompletionHandler) {
- self.closeCompletionHandler(0, @"离开聊天队列成功");
- self.closeCompletionHandler = nil;
- }
- return;
- }
-
- NSLog(@"被关闭连接,code:%ld,reason:%@,wasClean:%d",code,reason,wasClean);
-
- [self destoryHeartBeat]; //断开连接时销毁心跳
-
- //判断网络环境
- if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) //没有网络
- {
- [self noNetWorkStartTestingTimer];//开启网络检测
- }
- else //有网络
- {
- [self reConnectServer];//连接失败就重连
- }
- }
- //该函数是接收服务器发送的pong消息,其中最后一个参数是接受pong消息的
- -(void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData*)pongPayload
- {
- NSString* reply = [[NSString alloc] initWithData:pongPayload encoding:NSUTF8StringEncoding];
- NSLog(@"reply === 收到后台心跳回复 Data:%@",reply);
- }
- //收到服务器发来的数据
- - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
- {
- /// 这里使用Gzip解压 有些公司返回不是压缩包 根据实际处理
- NSData *datas = [LFCGzipUtility ungzipData:message];
- NSString * str = [[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
- if ([@"pong" isEqualToString:str]) {
- NSLog(@"验证心跳成功:%@", str);
- return;
- }
-
- /*根据具体的业务做具体的处理*/
- if ([self.delegate respondsToSelector:@selector(webSocketManagerDidReceiveMessageWithObject:)]) {
- [self.delegate webSocketManagerDidReceiveMessageWithObject:[str jsonValueDecoded]];
- }
-
- }
- @end
|