如果不是很明白的话,可以看下讲解 WebSocket
代码语言:javascript复制data(){
return{
websock:null
}
}
代码语言:javascript复制created() {
this.initWebSocket()//初始化weosocket
},
代码语言:javascript复制methods:{
/***初始化weosocket***/
initWebSocket(){
const wsuri = `ws://192.168.0.0:8083/xxxxxx/websocket`//后端提供地址
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
/***连接建立执行send方法发送数据***/
websocketonopen(){
// this.websocketsend()
},
/***连接失败重新连接***/
websocketonerror(){
this.initWebSocket()
},
/***接收数据***/
websocketonmessage(result){
console.log('接收到新消息=',result)
},
/***数据发送***/
websocketsend(data){//数据发送
console.log(data)
// this.websock.send(data)
},
/***断开连接***/
websocketclose(e){
console.log('断开连接', e)
}
}