chore: add basic reconnect

This commit is contained in:
Paul Makles
2023-04-08 20:39:56 +01:00
parent 932e73ab9e
commit 8067995f9d
2 changed files with 10 additions and 3 deletions
+1
View File
@@ -154,6 +154,7 @@ export class Client extends EventEmitter<Events> {
break;
case ConnectionState.Disconnected:
this.emit("disconnected");
setTimeout(() => this.connect(), 10000);
break;
}
});
+9 -3
View File
@@ -86,6 +86,7 @@ export class EventClient<T extends AvailableProtocols> extends EventEmitter<
connect(uri: string, token: string) {
this.disconnect();
this.setState(ConnectionState.Connecting);
this.#socket = new WebSocket(
`${uri}?version=${this.#protocolVersion}&format=${
this.#transportFormat
@@ -115,9 +116,13 @@ export class EventClient<T extends AvailableProtocols> extends EventEmitter<
}
};
let closed = false;
this.#socket.onclose = () => {
if (closed) return;
closed = true;
clearInterval(this.#heartbeatIntervalReference);
this.setState(ConnectionState.Disconnected);
this.disconnect();
};
}
@@ -126,9 +131,10 @@ export class EventClient<T extends AvailableProtocols> extends EventEmitter<
*/
disconnect() {
if (!this.#socket) return;
this.setState(ConnectionState.Disconnected);
this.#socket.close();
let socket = this.#socket;
this.#socket = undefined;
socket.close();
this.setState(ConnectionState.Disconnected);
}
/**