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