**
简单使用:
**
主要代码
initWebSocket() {
const wsuri = `ws:url`;
this.websock = new WebSocket(wsuri);//这里面的this都指向vue
this.websock.onopen = this.websocketopen;
this.websock.onmessage = this.websocketonmessage;
this.websock.onclose = this.websocketclose;
this.websock.onerror = this.websocketerror;
},
websocketopen() {//打开
// console.log("WebSocket连接成功")
},
websocketonmessage(e) { //数据接收
let ObjData = JSON.parse(e.data);
console.log(ObjData)
},
websocketclose() { //关闭
// console.log("WebSocket关闭");
},
websocketerror() { //失败
// console.log("WebSocket连接失败");
},
调用 看个人需求需要在什么地方调用
this.initWebSocket()
完成之后或者页面跳转别忘记关闭websocket
destroyed() {
this.websock.close() //离开路由之后断开websocket连接
},