diff --git a/src/util/BaseWSClient.ts b/src/util/BaseWSClient.ts index 24b7f76..c9832c9 100644 --- a/src/util/BaseWSClient.ts +++ b/src/util/BaseWSClient.ts @@ -509,7 +509,7 @@ export abstract class BaseWebsocketClient< const ws = this.getWs(wsKey); ws?.close(); if (force) { - safeTerminateWs(ws); + safeTerminateWs(ws, false); } } @@ -726,7 +726,7 @@ export abstract class BaseWebsocketClient< if (ws) { ws.close(); - safeTerminateWs(ws); + safeTerminateWs(ws, false); } if (!wasOpen) { diff --git a/src/util/websockets/websocket-util.ts b/src/util/websockets/websocket-util.ts index 2908d62..c4a4ee0 100644 --- a/src/util/websockets/websocket-util.ts +++ b/src/util/websockets/websocket-util.ts @@ -309,14 +309,26 @@ export const WS_ERROR_ENUM = { /** * #305: ws.terminate() is undefined in browsers. * This only works in node.js, not in browsers. - * Does nothing if `ws` is undefined. + * Does nothing if `ws` is undefined. Does nothing in browsers. */ -export function safeTerminateWs(ws?: WebSocket | unknown) { - // #305: ws.terminate() undefined in browsers - if (ws && typeof ws['terminate'] === 'function') { - ws.terminate(); +export function safeTerminateWs( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ws?: WebSocket | any, + fallbackToClose?: boolean, +): boolean { + if (!ws) { + return false; } + if (typeof ws['terminate'] === 'function') { + ws.terminate(); + return true; + } else if (fallbackToClose) { + ws.close(); + } + + return false; } + /** * WS API promises are stored using a primary key. This key is constructed using * properties found in every request & reply.