vue websoket 心跳机制使用笔记

data(){
  return {
     // path: "ws://192.168.1.85:8007/fmis-api/dispatch/1",
      path: window.WS + "/dispatch/1",
      // path:process.env.VUE_APP_WEBBASE_API+"/dispatch/1",
      socket: "",
      lockReconnect: false,//是否真正建立连接
      timeout: 28*1000,//30秒一次心跳
      timeoutObj: null,//心跳心跳倒计时
      serverTimeoutObj: null,//心跳倒计时
      timeoutnum: null,//断开 重连倒计时   
  }  
},
mounted{
    // 初始化———websocket
    this.initWesocket();
},
methods:{
     initWesocket() {
      if (typeof WebSocket === "undefined") {
        alert("您的浏览器不支持socket");
      } else {
        // 实例化socket
        this.socket = new WebSocket(this.path);
        // 监听socket连接
        this.socket.onopen = this.open;
        // 监听socket错误信息
        this.socket.onerror = this.error;
         this.socket.onclose = this.onclose;
        // 监听socket消息
        this.socket.onmessage = this.getMessage;
      }
    },
onclose(e) {
        console.log("连接关闭");
        console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean);
        //重连
        this.reconnect();
    },
      reconnect() {//重新连接

        if(this.lockReconnect) {
          return;
        };
        this.lockReconnect = true;
        //没连接上会一直重连,设置延迟避免请求过多
        this.timeoutnum && clearTimeout(this.timeoutnum);
        this.timeoutnum = setTimeout( ()=>{
          //新连接
          this.init();
          this.lockReconnect = false;
        },5000);
      },
      reset(){//重置心跳

        //清除时间
        clearTimeout(this.timeoutObj);
        clearTimeout(this.serverTimeoutObj);
        //重启心跳
        this.start();
      },
      start(){//开启心跳

        this.timeoutObj && clearTimeout(this.timeoutObj);
        this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
        this.timeoutObj = setTimeout(()=>{
          //这里发送一个心跳,后端收到后,返回一个心跳消息,
          if (this.socket.readyState == 1) {//如果连接正常
            this.socket.send("heartCheck");
          }else{//否则重连
            this.reconnect();
          }
          this.serverTimeoutObj = setTimeout(()=> {
            //超时关闭
            this.socket.close();
          }, this.timeout);

        }, this.timeout)
      },
      open() {
        console.log("socket连接成功");
        //开启心跳
        this.start();
      },
      error() {
        console.log("连接错误");
        //重连
        this.reconnect();
      },
 getMessage(msg) {
      console.log(msg.data, "有消息!!!");
      //处理具体业务
      // var arr = JSON.parse(msg.data);
      // console.log(typeof arr,"类型");
      if(this.userName=='0003'){
          //收到服务器信息,心跳重置
        this.reset();
         if (msg.data.includes("error:")) {
        return this.$message.error(msg.data.replace("error:", ""));
        } else {
          let arr = [];
          let s1 = msg.data.split(",");
          s1.forEach((item, i) => {
            // console.log(item);
            let obj = {};
            obj.id = item;
            arr.push(obj);
          });
          console.log(arr, "数组形式");
          if (arr != null && arr.length > 0) {
            this.onSubmit("search");
            this.carList = arr;
            console.log(typeof this.carList, "类型");
            console.log(this.carList);
            this.carList.forEach((item) => {
              console.log(item, "批量打印派车单ID");
              this.PrintDispatchList(item.id);
            });
          }
        }
      }

    },
    send() {
      this.socket.send("主动发送消息");
    },
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值