throttle reconnect

This commit is contained in:
guochao 2024-06-04 11:57:17 +08:00
parent ee339c82b3
commit 79b34139b5
Signed by: guochao
GPG Key ID: 79F7306D2AA32FC3

View File

@ -42,6 +42,8 @@
this.ws = null; this.ws = null;
this.reconnectTimeout = null;
this.event = new EventTarget(); this.event = new EventTarget();
} }
@ -157,11 +159,21 @@
let ws = new WebSocket(protocol + location.host + "/ws"); let ws = new WebSocket(protocol + location.host + "/ws");
ws.addEventListener("error", (ev) => { ws.addEventListener("error", (ev) => {
console.error("ws error", ev) console.error("ws error", ev)
this.reconnect() if(this.reconnectTimeout) {
return
}
this.reconnectTimeout = setTimeout(() => {
this.reconnect()
}, 3000)
}) })
ws.addEventListener("close", () => { ws.addEventListener("close", () => {
console.warn("ws closed") console.warn("ws closed")
this.reconnect() if (this.reconnectTimeout) {
return
}
this.reconnectTimeout = setTimeout(() => {
this.reconnect()
}, 3000)
}) })
ws.addEventListener("message", ({ data }) => { ws.addEventListener("message", ({ data }) => {
@ -172,6 +184,8 @@
this.kickoff() this.kickoff()
}) })
this.ws = ws; this.ws = ws;
this.reconnectTimeout = null;
} }
ChatRoom.prototype.send_message = function (message) { ChatRoom.prototype.send_message = function (message) {