This commit is contained in:
X1627315083
2024-09-30 17:59:24 +08:00
parent d661159c8a
commit 02fe8147c9
17 changed files with 92 additions and 26 deletions

View File

@@ -1,3 +1,4 @@
import { setCookie, getCookie, WriteCookie,clonAllCookie } from "@/tool/cookie";
import MyEvent from "@/tool/myEvents";
class MyWs {
constructor() {
@@ -5,10 +6,13 @@ class MyWs {
// this.ws = new WebSocket();
let http = process.env.VUE_APP_BASE_URL.replace(/^https?:\/\//, '')
this.messageUrl = http+'/notification'
this.wsUrl = ''
this.reconnectionTime = null
}
linkWs(url) {
if (this.ws && this.ws.readyState == 1) return
this.ws = new WebSocket(url)
if(!this.wsUrl)this.wsUrl = url +`/${JSON.parse(getCookie("userInfo")).userId}`
this.ws = new WebSocket(this.wsUrl)
this.ws.onmessage = (e)=>{
try {
MyWs.receiveText(JSON.parse(e.data))
@@ -16,6 +20,18 @@ class MyWs {
console.error('websocket',error)
}
}
this.ws.onclose = (e)=>{
reconnection()
}
this.ws.onerror = (e)=>{
reconnection()
}
}
reconnection(){
clearTimeout(this.reconnectionTime)
this.reconnectionTime = setTimeout(()=>{
this.linkWs(this.wsUrl)
},1000)
}
static receiveText(data,call){
MyEvent.emit('getMessage',data)