mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-20 20:16:06 -04:00
chore: add basic reconnect
This commit is contained in:
@@ -154,6 +154,7 @@ export class Client extends EventEmitter<Events> {
|
||||
break;
|
||||
case ConnectionState.Disconnected:
|
||||
this.emit("disconnected");
|
||||
setTimeout(() => this.connect(), 10000);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user