This commit is contained in:
X1627315083
2024-12-18 17:38:43 +08:00
90 changed files with 3220 additions and 865 deletions

View File

@@ -9,6 +9,7 @@ class MyWs {
this.wsUrl = ''
this.reconnectionTime = null
this.times = 0
this.sendPINGTime = null
}
linkWs(url) {
if(!(JSON.parse(getCookie("userInfo"))))return
@@ -17,19 +18,23 @@ class MyWs {
this.ws = new WebSocket(this.wsUrl)
this.ws.onmessage = (e)=>{
try {
MyWs.receiveText(JSON.parse(e.data))
let rv = JSON.parse(e.data)
if(rv == "PONG")return
this.times = 0
MyWs.receiveText(rv)
} catch (error) {
console.error('websocket',error)
}
}
clearInterval(this.sendPINGTime)
this.sendPINGTime = setInterval(()=>{
this.send({text:'PING'})
// },1000)
},1000*60*2)
this.ws.onclose = (e)=>{
console.log('',e);
console.log('错误');
this.reconnection()
}
this.ws.onerror = (e)=>{
console.log('失败',e);
this.reconnection()
}
}
@@ -37,6 +42,7 @@ class MyWs {
this.times++
clearTimeout(this.reconnectionTime)
if(this.times>=10)return
clearInterval(this.sendPINGTime)
this.reconnectionTime = setTimeout(()=>{
this.linkWs(this.wsUrl)
},1000)
@@ -70,7 +76,12 @@ class MyWs {
}
}
send(obj) {
this.ws.send(JSON.stringify(obj))
if(this.ws.readyState == 1){
this.ws.send(JSON.stringify(obj))
}else{
clearTimeout(this.sendPINGTime)
this.reconnection()
}
}
close(){
if (this.ws && this.ws.readyState == 1) {