Skip to content

Commit

Permalink
chore: websocket error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-pingin committed Oct 17, 2023
1 parent 4275420 commit 6bffd62
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/utils/websocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ export class WebsocketHandler {
};

this.#websocket.onerror = (error: WebSocket.ErrorEvent) => {
this.#errorHandlers.forEach((handler) => {
handler(error);
});
this._handleError(error);
};

this.#websocket.onmessage = async (data: WebSocket.MessageEvent) => {
Expand All @@ -139,9 +137,8 @@ export class WebsocketHandler {
const errorWebsocketMessage = message as ErrorWebsocketMessage;
const error: Error = errorWebsocketMessage.payload;

this.#errorHandlers.forEach((handler) => {
handler(error);
});
this._handleError(error);

break;
}
case WebsocketEvent.UPDATE: {
Expand Down Expand Up @@ -197,7 +194,10 @@ export class WebsocketHandler {
private async _ensureIsConnected(): Promise<boolean> {
if (!this.#websocket) {
this._connect();
} else if (this.#websocket.readyState !== WebSocket.OPEN) {
} else if (
this.#websocket.readyState !== WebSocket.CONNECTING &&
this.#websocket.readyState !== WebSocket.OPEN
) {
this._reconnect();
}
return await this._awaitConnection();
Expand All @@ -208,17 +208,21 @@ export class WebsocketHandler {
while (!this.#websocket || this.#websocket.readyState !== WebSocket.OPEN) {
const elapsed = Date.now() - start;
if (elapsed > this.#connectTimeoutMillis) {
const error = new Error(
`Timeout connecting to ${this.#url} after ${elapsed}ms`
this._handleError(
new Error(`Timeout connecting to ${this.#url} after ${elapsed}ms`)
);
this.#errorHandlers.forEach((handler) => {
handler(error);
});

return false;
}

await new Promise((resolve) => setTimeout(resolve, 10));
}
return true;
}

private _handleError(error: Error) {
this.#errorHandlers.forEach((handler) => {
handler(error);
});
}
}

0 comments on commit 6bffd62

Please sign in to comment.