zoukankan      html  css  js  c++  java
  • websocket 断线重连

    服务端为swoole 的websocket

    客户端js代码:

    //1.创建websocket客户端
      var wsServer = 'ws://ip/';
      var limitConnect = 3;  // 断线重连次数
      var timeConnect =0;
      webSocketInit(wsServer);
    
      //socket初始化
      function webSocketInit(service){
        var ws = new WebSocket(service);
        ws.onopen = function () {
          console.log("已连接TCP服务器");
        };
        ws.onmessage = function (msg) {
          console.log(msg);
        };
        ws.onclose = function () {
          console.log('服务器已经断开');
          reconnect(service);
        };
        ws.onerror = function (err) {
          //console.log("服务器报错:");
          reconnect(service);
        };
    
        // 重连
        function reconnect(service) {
          // lockReconnect加锁,防止onclose、onerror两次重连
          if(limitConnect>0){
            if(localStorage.getItem('lockReconnect')!=true){
              localStorage.setItem("lockReconnect",1);
              limitConnect --;
              timeConnect ++;
              console.log("第"+timeConnect+"次重连");
              // 进行重连
              setTimeout(function(){
                webSocketInit(service);
                localStorage.removeItem("lockReconnect");
              },2000);
            }
          }else{
            console.log("TCP连接已超时");
          }
        }
    
        // 心跳 * 回应
        setInterval(function(){
          websocket.send('');
        }, 1000*100);

    注意:

    1.onclose、onerror出现两个,tcp重连的时候会重连两次;为避免这种情况,需要进行加锁lockReconnect

    2.limitConnect 断线重连次数;timeConnect从0次开始播报

    效果:

    完整代码:去掉了onerror,不需要加锁

    //1.创建websocket客户端
      var wsServer = 'ws://ip/';
      var limitConnect = 3;  // 断线重连次数
      var timeConnect = 0;
      webSocketInit(wsServer);
    
      //socket初始化
      function webSocketInit(service){
        var ws = new WebSocket(service);
        ws.onopen = function () {
          console.log("已连接TCP服务器");
        };
        ws.onmessage = function (msg) {
          console.log(msg);
        };
        ws.onclose = function () {
          console.log('服务器已经断开');
          reconnect(service);
        };
    
        // 重连
        function reconnect(service) {
          // lockReconnect加锁,防止onclose、onerror两次重连
          if(limitConnect>0){
              limitConnect --;
              timeConnect ++;
              console.log("第"+timeConnect+"次重连");
              // 进行重连
              setTimeout(function(){
                webSocketInit(service);
              },2000);
    
          }else{
            console.log("TCP连接已超时");
          }
        }
    
        // 心跳 * 回应
        setInterval(function(){
          websocket.send('');
        }, 1000*100);

    作者:狂奔的蜗牛,转载请注明出处

  • 相关阅读:
    程序员自我【营销】,如何打造个人【品牌】
    程序员应该怎样和领导相处?
    程序员必备能力——晋升之道
    聊一聊 软件系统中的“热力学第二定律”
    程序员如何利用技术管理技巧
    技术人必须掌握能力——深度思考
    程序员逆袭之路——系列文章更新中
    程序员跳槽,该如何选择一家好公司
    C++-运行时类型信息,异常(day11)
    C++-多态,纯虚函数,抽象类,工厂模式,虚析构函数(day10)
  • 原文地址:https://www.cnblogs.com/wesky/p/12111929.html
Copyright © 2011-2022 走看看