您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在iOS中實現WebSocket長鏈接,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
WebSocket
WebSocket 是 HTML5 一種新的協議。它實現了瀏覽器與服務器全雙工通信,能更好的節省服務器資源和帶寬并達到實時通訊,它建立在 TCP 之上,同 HTTP 一樣通過 TCP 來傳輸數據,但是它和 HTTP 最大不同是:WebSocket 是一種雙向通信協議.
由于項目需要創建一個聊天室,需要通過長鏈接,和后臺保持通訊,進行聊天,并且實時進行熱點消息的推送.
目前Facebook的SocketRocket應該是目前最好的關于SocketRocket使用的框架了.而且簡單易用.
使用
一般一個項目在啟動后的某個時機會啟動創建一個長鏈接,如果需要多個就多次創建.如果只要一個就可以封裝為一個單例,全局使用.
可以使用podpod管理庫, 在podfile中加入
pod 'SocketRocket'
在使用命令行工具cd到當前工程 安裝
pod install
導入頭文件后即可使用.
為求穩定,我的做法是copy的FaceBook里SocketRocket庫到項目里. --> SocketRocket地址
1.首先創建一個名為 WebSocketManager 的單例類,
+(instancetype)shared;
2.創建一個枚舉,分別表示WebSocket的鏈接狀態
typedef NS_ENUM(NSUInteger,WebSocketConnectType){ WebSocketDefault = 0, //初始狀態,未連接,不需要重新連接 WebSocketConnect, //已連接 WebSocketDisconnect //連接后斷開,需要重新連接 };
3.創建連接
//建立長連接 - (void)connectServer;
4.處理連接成功的結果;
-(void)webSocketDidOpen:(RMWebSocket *)webSocket; //連接成功回調
5.處理連接失敗的結果
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;//連接失敗回調
6.接收消息
///接收消息回調,需要提前和后臺約定好消息格式. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(nonnull NSString *)string
7.關閉連接
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;///關閉連接回調的代理
8.為保持長鏈接的連接狀態,需要定時向后臺發送消息,就是俗稱的:心跳包.
需要創建一個定時器,固定時間發送消息.
9.鏈接斷開情況處理:
首先判斷是否是主動斷開,如果是主動斷開就不作處理.
如果不是主動斷開鏈接,需要做重新連接的邏輯.
具體代碼如下:
WebSocketManager.h 中的代碼
#import <foundation foundation="" h=""> #import "RMWebSocket.h" typedef NS_ENUM(NSUInteger,WebSocketConnectType){ WebSocketDefault = 0, //初始狀態,未連接 WebSocketConnect, //已連接 WebSocketDisconnect //連接后斷開 }; @class WebSocketManager; @protocol WebSocketManagerDelegate <nsobject> - (void)webSocketManagerDidReceiveMessageWithString:(NSString *)string; @end NS_ASSUME_NONNULL_BEGIN @interface WebSocketManager : NSObject @property (nonatomic, strong) RMWebSocket *webSocket; @property(nonatomic,weak) id <websocketmanagerdelegate nbsp=""> delegate; @property (nonatomic, assign) BOOL isConnect; //是否連接 @property (nonatomic, assign) WebSocketConnectType connectType; +(instancetype)shared; - (void)connectServer;//建立長連接 - (void)reConnectServer;//重新連接 - (void)RMWebSocketClose;//關閉長連接 - (void)sendDataToServer:(NSString *)data;//發送數據給服務器 @end NS_ASSUME_NONNULL_END </websocketmanagerdelegate> </nsobject> </foundation>
WebSocketManager.m 中的代碼
#import "WebSocketManager.h" @interface WebSocketManager () <rmwebsocketdelegate> @property (nonatomic, strong) NSTimer *heartBeatTimer; //心跳定時器 @property (nonatomic, strong) NSTimer *netWorkTestingTimer; //沒有網絡的時候檢測網絡定時器 @property (nonatomic, assign) NSTimeInterval reConnectTime; //重連時間 @property (nonatomic, strong) NSMutableArray *sendDataArray; //存儲要發送給服務端的數據 @property (nonatomic, assign) BOOL isActivelyClose; //用于判斷是否主動關閉長連接,如果是主動斷開連接,連接失敗的代理中,就不用執行 重新連接方法 @end @implementation WebSocketManager +(instancetype)shared{ static WebSocketManager *_instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc]init]; }); return _instance; } - (instancetype)init { self = [super init]; if(self){ self.reConnectTime = 0; self.isActivelyClose = NO; self.sendDataArray = [[NSMutableArray alloc] init]; } return self; } //建立長連接 - (void)connectServer{ self.isActivelyClose = NO; self.webSocket.delegate = nil; [self.webSocket close]; _webSocket = nil; // self.webSocket = [[RMWebSocket alloc] initWithURL:[NSURL URLWithString:@"https://dev-im-gateway.runxsports.com/ws/token=88888888"]]; self.webSocket = [[RMWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://chat.workerman.net:7272"]]; self.webSocket.delegate = self; [self.webSocket open]; } - (void)sendPing:(id)sender{ [self.webSocket sendPing:nil error:NULL]; } #pragma mark -------------------------------------------------- #pragma mark - socket delegate ///開始連接 -(void)webSocketDidOpen:(RMWebSocket *)webSocket{ NSLog(@"socket 開始連接"); self.isConnect = YES; self.connectType = WebSocketConnect; [self initHeartBeat];///開始心跳 } ///連接失敗 -(void)webSocket:(RMWebSocket *)webSocket didFailWithError:(NSError *)error{ NSLog(@"連接失敗"); self.isConnect = NO; self.connectType = WebSocketDisconnect; DLog(@"連接失敗,這里可以實現掉線自動重連,要注意以下幾點"); DLog(@"1.判斷當前網絡環境,如果斷網了就不要連了,等待網絡到來,在發起重連"); DLog(@"3.連接次數限制,如果連接失敗了,重試10次左右就可以了"); //判斷網絡環境 if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable){ //沒有網絡 [self noNetWorkStartTestingTimer];//開啟網絡檢測定時器 }else{ //有網絡 [self reConnectServer];//連接失敗就重連 } } ///接收消息 -(void)webSocket:(RMWebSocket *)webSocket didReceiveMessageWithString:(NSString *)string{ NSLog(@"接收消息---- %@",string); if ([self.delegate respondsToSelector:@selector(webSocketManagerDidReceiveMessageWithString:)]) { [self.delegate webSocketManagerDidReceiveMessageWithString:string]; } } ///關閉連接 -(void)webSocket:(RMWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean{ self.isConnect = NO; if(self.isActivelyClose){ self.connectType = WebSocketDefault; return; }else{ self.connectType = WebSocketDisconnect; } DLog(@"被關閉連接,code:%ld,reason:%@,wasClean:%d",code,reason,wasClean); [self destoryHeartBeat]; //斷開連接時銷毀心跳 //判斷網絡環境 if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable){ //沒有網絡 [self noNetWorkStartTestingTimer];//開啟網絡檢測 }else{ //有網絡 NSLog(@"關閉連接"); _webSocket = nil; [self reConnectServer];//連接失敗就重連 } } ///ping -(void)webSocket:(RMWebSocket *)webSocket didReceivePong:(NSData *)pongData{ NSLog(@"接受pong數據--> %@",pongData); } #pragma mark - NSTimer //初始化心跳 - (void)initHeartBeat{ //心跳沒有被關閉 if(self.heartBeatTimer) { return; } [self destoryHeartBeat]; dispatch_main_async_safe(^{ self.heartBeatTimer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(senderheartBeat) userInfo:nil repeats:true]; [[NSRunLoop currentRunLoop]addTimer:self.heartBeatTimer forMode:NSRunLoopCommonModes]; }) } //重新連接 - (void)reConnectServer{ if(self.webSocket.readyState == RM_OPEN){ return; } if(self.reConnectTime > 1024){ //重連10次 2^10 = 1024 self.reConnectTime = 0; return; } WS(weakSelf); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.reConnectTime *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if(weakSelf.webSocket.readyState == RM_OPEN && weakSelf.webSocket.readyState == RM_CONNECTING) { return; } [weakSelf connectServer]; // CTHLog(@"正在重連......"); if(weakSelf.reConnectTime == 0){ //重連時間2的指數級增長 weakSelf.reConnectTime = 2; }else{ weakSelf.reConnectTime *= 2; } }); } //發送心跳 - (void)senderheartBeat{ //和服務端約定好發送什么作為心跳標識,盡可能的減小心跳包大小 WS(weakSelf); dispatch_main_async_safe(^{ if(weakSelf.webSocket.readyState == RM_OPEN){ [weakSelf sendPing:nil]; } }); } //沒有網絡的時候開始定時 -- 用于網絡檢測 - (void)noNetWorkStartTestingTimer{ WS(weakSelf); dispatch_main_async_safe(^{ weakSelf.netWorkTestingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakSelf selector:@selector(noNetWorkStartTesting) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:weakSelf.netWorkTestingTimer forMode:NSDefaultRunLoopMode]; }); } //定時檢測網絡 - (void)noNetWorkStartTesting{ //有網絡 if(AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus != AFNetworkReachabilityStatusNotReachable) { //關閉網絡檢測定時器 [self destoryNetWorkStartTesting]; //開始重連 [self reConnectServer]; } } //取消網絡檢測 - (void)destoryNetWorkStartTesting{ WS(weakSelf); dispatch_main_async_safe(^{ if(weakSelf.netWorkTestingTimer) { [weakSelf.netWorkTestingTimer invalidate]; weakSelf.netWorkTestingTimer = nil; } }); } //取消心跳 - (void)destoryHeartBeat{ WS(weakSelf); dispatch_main_async_safe(^{ if(weakSelf.heartBeatTimer) { [weakSelf.heartBeatTimer invalidate]; weakSelf.heartBeatTimer = nil; } }); } //關閉長連接 - (void)RMWebSocketClose{ self.isActivelyClose = YES; self.isConnect = NO; self.connectType = WebSocketDefault; if(self.webSocket) { [self.webSocket close]; _webSocket = nil; } //關閉心跳定時器 [self destoryHeartBeat]; //關閉網絡檢測定時器 [self destoryNetWorkStartTesting]; } //發送數據給服務器 - (void)sendDataToServer:(NSString *)data{ [self.sendDataArray addObject:data]; //[_webSocket sendString:data error:NULL]; //沒有網絡 if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { //開啟網絡檢測定時器 [self noNetWorkStartTestingTimer]; } else //有網絡 { if(self.webSocket != nil) { // 只有長連接OPEN開啟狀態才能調 send 方法,不然會Crash if(self.webSocket.readyState == RM_OPEN) { // if (self.sendDataArray.count > 0) // { // NSString *data = self.sendDataArray[0]; [_webSocket sendString:data error:NULL]; //發送數據 // [self.sendDataArray removeObjectAtIndex:0]; // // } } else if (self.webSocket.readyState == RM_CONNECTING) //正在連接 { DLog(@"正在連接中,重連后會去自動同步數據"); } else if (self.webSocket.readyState == RM_CLOSING || self.webSocket.readyState == RM_CLOSED) //斷開連接 { //調用 reConnectServer 方法重連,連接成功后 繼續發送數據 [self reConnectServer]; } } else { [self connectServer]; //連接服務器 } } } @end </rmwebsocketdelegate>
注意點
我們在發送消息之前,也就是調用 senderheartBeat/ sendDataToServer:方法之前,一定要判斷當前scoket是否連接,如果不是連接狀態,程序則會crash。
iOS手機屏幕息屏或者回主頁的時候有可能會造成鏈接斷開,我這邊的處理是在回到屏幕的時候,判斷狀態,如果已經斷開,就重新連接.
在 AppDelegate 中:
- (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. if ([WebSocketManager shared].connectType == WebSocketDisconnect) { [[WebSocketManager shared] connectServer]; } }
看完上述內容,你們對怎么在iOS中實現WebSocket長鏈接有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。