websocket主要分为四步
1.打开数据
2.接收数据
3.断开连接
4.连接失败
data() {
return {
websock: null
}
}
初始化websocket
initWebpack() {
const wsuri = "这里是websocket接口地址"
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(e) {
console.log("WebSocket连接成功")
},
数据接收
websocketonmessage(response) {
console.log(response)
}
关闭连接
websocketclose() {
console.log("WebSocket关闭");
},
连接失败
websocketerror() {
console.log("WebSocket连接失败");
},