this.ws = new WebSocket("ws://ip:端口号");
this.ws.onopen = () => {
console.log("ws连接状态:" + this.ws.readyState);
if(this.ws.readyState == 1){
let obj = {};
this.ws.send(JSON.stringify(obj));
}
};
this.ws.onmessage = (data) => {
console.log("接收到来自服务器的消息:",data);
let result = JSON.parse(data.data);
};
this.ws.onclose = () => {
console.log("ws连接状态:" + this.ws.readyState);
};
this.ws.onerror = function (error) {
console.log("连接错误:" + error);
};
if (this.ws) {
this.ws.close();
console.log("WebSocket 已关闭");
}
function() {
return new Promise((resolve, reject)=>{
resolve(true)
reject(false)
})
}
await function().then(()=>{
))。catch((error)=>{
console.log("error",error)
})