Vue-cli I websocket encapsulated, how do I listen in the component to get the data

problem description

websocket, this.socketApi.controlsendSock(controlData)
js
websocket2

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)
websocket.js

function websocket(){
    var url = "wss://"+getPostUrl()+":9567/deviceop";
    ws =new WebSocket(url);
    ws.binaryType = "arraybuffer";
    ws.onmessage = function(event){
        console.log("data === " + event.data);
        if(event.data instanceof ArrayBuffer){
            var buffer = event.data;
            var data = parse(buffer)
            var newdata = JSON.parse(data)
        }
    }
    ws.onopen = function (event) {
        console.log("")
    }
    ws.onclose = function(event){
        console.log("")
    }
    ws.onerror = function () {
        // wsData="qweqweuqeg iqwge iqgwiug"
        console.log("WebSocket");
    }

}

function controlsendSock(controlData){
    ws.send(controlData)
}

websocket();
export{ws,controlsendSock}

what result do you expect? What is the error message actually seen?

 newdata  

Mar.11,2022

controlData , or directly return a Promise receive a message to trigger.
of course, you have to make an one-to-one correspondence between onmessage and send messages. What I do is to create a unique id background callback to return the id when send, so that I can locate the message and not be afraid to repeat it

.
Menu