data()
{
return{
websockets:""
}
},
methods:{
initWebSockets() {
if (typeof WebSocket === "undefined") {
return false;
}
let wsuri;
wsuri = 'ws://-------------------';
this.websockets = new WebSocket(wsuri);
this.websockets.onopen = this.websocketonopens;
this.websockets.onmessage = this.websocketonmessages;
this.websockets.onerror = this.websocketonerrors;
this.websockets.onclose = this.websocketcloses;
},
websocketonopens() {
console.log("WebSocket连接成功");
},
websocketonmessages(e) {
let data1Json = JSON.parse(e.data);
new Promise((resolve) => {
setTimeout(() => {
setTimeout(() => {
if (data1Json.id) {
}
}, 40)
resolve()
}, 40)
}).then(() => {
return new Promise((resolve) => {
resolve()
})
})
},
websocketonerrors(e) {
console.log(`连接失败的信息:`, e);
this.initWebSockets();
},
websocketcloses(e) {
console.log("断开连接", e);
}
},
destroyed(){
if(this.websockets){
this.websockets.close()
}
}
|