failed to connect using native websocket in vue
< template >
<button @click="websocketonmessage()"></button>
< / template >
< script >
export default {
data () {
return {
websock:null
};
},
methods: {
// webscoket
initWebSocket(){
//websocket
const wsurl = "";
this.websock = new WebSocket(wsurl);
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onmessage = this.websocketonmessage;
this.websocket = this.websocket;
this.websock.onclose = this.websocketclose;
},
websocket(){
this.initWebSocket();
},
websocketonopen(e){
console.log("WebSocket",e);
},
websocketonerror(e) { //
console.log ("connection error", e);
},
websocketonmessage(res){
console.log("",res.data);
},
websocketclose(e){
console.log("",e)
},
},
created () {
this.initWebSocket();
},
destroyed(){
this.websocketclose();
}
}
< / script >