created:
this.contactSocket()
methods
contactSocket(){
let webSocket = null;
let socketOpen = false;
let that=this
if ("WebSocket" in window) {
webSocket = new WebSocket("ws://后台给的url");
webSocket.onopen = function (){
console.log("连接成功!");
webSocket.send(that.token)
socketOpen = true
};
webSocket.onmessage = function (evt) {
var received_msg = evt.data;
console.log("接受消息:" + received_msg);
};
webSocket.onclose = function () {
console.log("连接关闭!");
that.reconnect()
};
webSocket.onerror = function () {
console.log("连接异常!");
};
}
},
// 重连
reconnect(){
console.log('重新连接')
setInterval(this.contactSocket(), 10000)
},
直接换好链接调用就行了,非常简单