diff --git a/src/websocket-client.ts b/src/websocket-client.ts index 24eb687..2d63d46 100644 --- a/src/websocket-client.ts +++ b/src/websocket-client.ts @@ -276,6 +276,7 @@ export class WebsocketClient extends EventEmitter { public closeAll(force?: boolean) { const keys = this.wsStore.getKeys(); + this.logger.info(`Closing all ws connections: ${keys}`); keys.forEach((key) => { this.close(key, force); }); @@ -441,13 +442,22 @@ export class WebsocketClient extends EventEmitter { break; default: - this.logger.error( - `${context} due to unexpected response error: "${ - error?.msg || error?.message || error - }"`, - { ...loggerCategory, wsKey, error } - ); - this.executeReconnectableClose(wsKey, 'unhandled onWsError'); + if ( + this.wsStore.getConnectionState(wsKey) !== + WsConnectionStateEnum.CLOSING + ) { + this.logger.error( + `${context} due to unexpected response error: "${ + error?.msg || error?.message || error + }"`, + { ...loggerCategory, wsKey, error } + ); + this.executeReconnectableClose(wsKey, 'unhandled onWsError'); + } else { + this.logger.info( + `${wsKey} socket forcefully closed. Will not reconnect.` + ); + } break; } this.emit('error', error); diff --git a/test/spot/ws.private.v1.test.ts b/test/spot/ws.private.v1.test.ts index 5a98082..617e21b 100644 --- a/test/spot/ws.private.v1.test.ts +++ b/test/spot/ws.private.v1.test.ts @@ -9,6 +9,7 @@ import { getSilentLogger, waitForSocketEvent, WS_OPEN_EVENT_PARTIAL, + fullLogger, } from '../ws.util'; describe('Private Spot V1 Websocket Client', () => { @@ -30,6 +31,7 @@ describe('Private Spot V1 Websocket Client', () => { beforeAll(() => { wsClient = new WebsocketClient( wsClientOptions, + // fullLogger getSilentLogger('expectSuccess') ); logAllEvents(wsClient); diff --git a/test/unified-margin/ws.public.perp.usdc.test.ts b/test/unified-margin/ws.public.perp.usdc.test.ts index cb1a31f..86247f7 100644 --- a/test/unified-margin/ws.public.perp.usdc.test.ts +++ b/test/unified-margin/ws.public.perp.usdc.test.ts @@ -28,10 +28,6 @@ describe('Public Unified Margin Websocket Client (Perps - USDC)', () => { wsClient.connectPublic(); }); - afterAll(() => { - wsClient.closeAll(true); - }); - it('should open a public ws connection', async () => { const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); try { @@ -42,6 +38,8 @@ describe('Public Unified Margin Websocket Client (Perps - USDC)', () => { } catch (e) { expect(e).toBeFalsy(); } + + wsClient.closeAll(true); }); // TODO: are there USDC topics? This doesn't seem to work