需求
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