websocket vue (including failed reconnection)

need

1. Configure websocket

2. Heartbeat disconnection and reconnection

3. Release resources

1. Configure websocket

npm i websocket

Initialize websocket and carry 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

Guess you like

Origin blog.csdn.net/weixin_42066070/article/details/130863344