websocket vue (包含失败重连)

需求

1.配置websocket

2.心跳断开重连

3.释放资源

1.配置websocket

npm i websocket

初始化websocket,并且携带上token

  mounted() {
    // 有window再发起
    this.initWebSocket();
  },

methods: {
    initWebSocket() {
      let _this = this;
      //判断当前浏览器是否支持WebSocket
      if ("WebSocket" in window) {
        // debugger;
        let token = Cookies.get('trawms-token')
        console.log(token)
        console.log(_this.websoketUrl)
        if (token) {
          if(!this.websock){
            // /gateway/instrument/ws
            this.websock = new WebSocket(
              _this.websoketUrl + "/socket",
              [token]
            );
          }
        }
      } else {
        this.$message.warning("不支持建立socket连接");
        return false;
      }
      this.websock.onopen = this.websocketOnopen;
      this.webs

猜你喜欢

转载自blog.csdn.net/weixin_42066070/article/details/130863344