调整ws bug
This commit is contained in:
@@ -8,6 +8,7 @@ class MyWs {
|
||||
this.messageUrl = http+'/notification'
|
||||
this.wsUrl = ''
|
||||
this.reconnectionTime = null
|
||||
this.sendPINGTime = null
|
||||
}
|
||||
linkWs(url) {
|
||||
if(!(JSON.parse(getCookie("userInfo"))))return
|
||||
@@ -16,25 +17,29 @@ 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
|
||||
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()
|
||||
}
|
||||
}
|
||||
reconnection(){
|
||||
clearTimeout(this.reconnectionTime)
|
||||
clearInterval(this.sendPINGTime)
|
||||
this.reconnectionTime = setTimeout(()=>{
|
||||
this.wsUrl = url +`/${JSON.parse(getCookie("userInfo")).userId}`
|
||||
this.linkWs(this.wsUrl)
|
||||
},1000)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user