data() {
return {
websock: null,
}
},created() {
this.initWebSocket();
},
destroyed() {
this.websock.close()
},
methods: {
initWebSocket(){
const wsuri = "ws://127.0.0.1:8080";
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;
},
websocketonopen(){
let actions = {"test":"12345"};
this.websocketsend(JSON.stringify(actions));
},
websocketonerror(){
this.initWebSocket();
},
websocketonmessage(e){
const redata = JSON.parse(e.data);
},
websocketsend(Data){
this.websock.send(Data);
},
websocketclose(e){
console.log('断开连接',e);
},
},
转自:Sven0706
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)