reconnect

This commit is contained in:
Lily Tsuru 2024-04-03 20:05:04 -04:00
parent 87b75c2ea1
commit 8bd36f29b2

View file

@ -10,6 +10,12 @@ type UserRecord = {
let turnInt: number = -1; let turnInt: number = -1;
async function sleep(ms: number) : Promise<void> {
return new Promise((res) => {
setTimeout(() => res(), ms);
});
}
function waitingTimer(text, ms, dot: boolean = true) { function waitingTimer(text, ms, dot: boolean = true) {
let dots = ''; let dots = '';
let timer = document.querySelector('.turn-timer') as HTMLDivElement; let timer = document.querySelector('.turn-timer') as HTMLDivElement;
@ -212,17 +218,25 @@ class SocketClient {
} }
} }
private onWsClose() { private async onWsClose() {
// backoff + reconnect?
this.websocket.removeEventListener("open", this.onWsOpen); this.websocket.removeEventListener("open", this.onWsOpen);
this.websocket.removeEventListener("message", this.onWsMessage); this.websocket.removeEventListener("message", this.onWsMessage);
this.websocket.removeEventListener("close", this.onWsClose); this.websocket.removeEventListener("close", this.onWsClose);
this.websocket = null; this.websocket = null;
// reset state // reset state
this.resetState();
// reconnect
await sleep(1000);
this.connect();
}
private resetState() {
this.selfNamed = false; this.selfNamed = false;
this.name = ""; this.name = "";
this.userList = []; this.userList = [];
this.updateUserCount();
} }
async SendMessage(messageGenerator: (encoder: Shared.MessageEncoder) => ArrayBuffer) { async SendMessage(messageGenerator: (encoder: Shared.MessageEncoder) => ArrayBuffer) {