How to solve the failure of using native websocket connection in vue?

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 >

clipboard.png

clipboard.png

clipboard.png

Sep.07,2021

this is that the server is not connected, and it has nothing to do with your code. See if the address is written correctly


is the reason why hosts is not configured. It has been solved!

Menu