diff --git a/examples/ws-public.ts b/examples/ws-public.ts index 1313cd9..bfe8528 100644 --- a/examples/ws-public.ts +++ b/examples/ws-public.ts @@ -6,35 +6,41 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src'; (async () => { const logger = { ...DefaultLogger, - // silly: () => {}, + silly: (...params) => console.log('silly', ...params), }; const wsClient = new WebsocketClient( { // key: key, // secret: secret, - // market: 'linear', // market: 'inverse', + market: 'linear', // market: 'spot', - market: 'usdcOption', + // market: 'spotv3', + // market: 'usdcOption', + // market: 'usdcPerp', + // market: 'unifiedPerp', + // market: 'unifiedOption', }, logger ); wsClient.on('update', (data) => { - console.log('raw message received ', JSON.stringify(data, null, 2)); + console.log('raw message received ', JSON.stringify(data)); + // console.log('raw message received ', JSON.stringify(data, null, 2)); }); wsClient.on('open', (data) => { console.log('connection opened open:', data.wsKey); - if (data.wsKey === WS_KEY_MAP.spotPublic) { - // Spot public. - // wsClient.subscribePublicSpotTrades('BTCUSDT'); - // wsClient.subscribePublicSpotTradingPair('BTCUSDT'); - // wsClient.subscribePublicSpotV1Kline('BTCUSDT', '1m'); - // wsClient.subscribePublicSpotOrderbook('BTCUSDT', 'full'); - } + // if (data.wsKey === WS_KEY_MAP.spotPublic) { + // // Spot public, but not recommended - use spotv3 client instead + // // The old spot websockets dont automatically resubscribe if they disconnect + // // wsClient.subscribePublicSpotTrades('BTCUSDT'); + // // wsClient.subscribePublicSpotTradingPair('BTCUSDT'); + // // wsClient.subscribePublicSpotV1Kline('BTCUSDT', '1m'); + // // wsClient.subscribePublicSpotOrderbook('BTCUSDT', 'full'); + // } }); wsClient.on('response', (data) => { console.log('log response: ', JSON.stringify(data, null, 2)); @@ -52,10 +58,16 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src'; // Linear wsClient.subscribe('trade.BTCUSDT'); + // Spot V3 + // usdc options - wsClient.subscribe(`recenttrades.BTC`); - wsClient.subscribe(`recenttrades.ETH`); - wsClient.subscribe(`recenttrades.SOL`); + // wsClient.subscribe(`recenttrades.BTC`); + // wsClient.subscribe(`recenttrades.ETH`); + // wsClient.subscribe(`recenttrades.SOL`); + + // usdc perps + + // unified perps // setTimeout(() => { // console.log('unsubscribing'); diff --git a/src/websocket-client.ts b/src/websocket-client.ts index 6049e23..80cba8a 100644 --- a/src/websocket-client.ts +++ b/src/websocket-client.ts @@ -188,18 +188,22 @@ export class WebsocketClient extends EventEmitter { return this.options.testnet === true; } - public close(wsKey: WsKey) { + public close(wsKey: WsKey, force?: boolean) { this.logger.info('Closing connection', { ...loggerCategory, wsKey }); this.setWsState(wsKey, WsConnectionStateEnum.CLOSING); this.clearTimers(wsKey); - this.getWs(wsKey)?.close(); + const ws = this.getWs(wsKey); + ws?.close(); + if (force) { + ws?.terminate(); + } } - public closeAll() { + public closeAll(force?: boolean) { const keys = this.wsStore.getKeys(); keys.forEach((key) => { - this.close(key); + this.close(key, force); }); } @@ -460,6 +464,10 @@ export class WebsocketClient extends EventEmitter { } private ping(wsKey: WsKey) { + if (this.wsStore.get(wsKey, true).activePongTimer) { + return; + } + this.clearPongTimer(wsKey); this.logger.silly('Sending ping', { ...loggerCategory, wsKey }); @@ -470,7 +478,8 @@ export class WebsocketClient extends EventEmitter { ...loggerCategory, wsKey, }); - this.getWs(wsKey)?.close(); + this.getWs(wsKey)?.terminate(); + delete this.wsStore.get(wsKey, true).activePongTimer; }, this.options.pongTimeout); } @@ -622,9 +631,9 @@ export class WebsocketClient extends EventEmitter { const msg = JSON.parse((event && event.data) || event); this.logger.silly('Received event', { - ...this.logger, + ...loggerCategory, wsKey, - msg: JSON.stringify(msg, null, 2), + msg: JSON.stringify(msg), }); // TODO: cleanme