From c567ef25c6f67561fdb270ace3ca64ccf48d71c8 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Mon, 5 Aug 2024 00:43:05 +0700 Subject: [PATCH] feat(packages/api): allow setting new options for `ClientWebsocketManager` --- packages/api/src/classes/ClientWebSocket.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/api/src/classes/ClientWebSocket.ts b/packages/api/src/classes/ClientWebSocket.ts index fd6e9d0..87b75d2 100755 --- a/packages/api/src/classes/ClientWebSocket.ts +++ b/packages/api/src/classes/ClientWebSocket.ts @@ -17,7 +17,7 @@ import { type RawData, WebSocket } from 'ws' * This is the only relevant class for the time being. But in the future, there may be more classes to handle different protocols of the API. */ export class ClientWebSocketManager { - readonly url: string + url: string timeout: number connecting = false @@ -33,6 +33,21 @@ export class ClientWebSocketManager { this.timeout = options.timeout ?? 10000 } + /** + * Sets the URL to connect to + * + * **Requires a reconnect to take effect** + */ + async setOptions({ url, timeout }: Partial, autoReconnect = true) { + if (url) this.url = url + this.timeout = timeout ?? this.timeout + + if (autoReconnect) { + this.disconnect(true) + await this.connect() + } + } + /** * Connects to the WebSocket API * @returns A promise that resolves when the client is ready