iOS WebSocket长链接的实现方法

2020-01-21 07:42:19于海丽

注意点

我们在发送消息之前,也就是调用  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];    
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。