feat(): v2 public websockets & reusable base class for websockets

This commit is contained in:
Tiago Siebler
2023-11-14 15:39:53 +00:00
parent ea8fac777e
commit 8346e88a3b
11 changed files with 1054 additions and 97 deletions

View File

@@ -1,21 +1,19 @@
import { EventEmitter } from 'events';
import WebSocket from 'isomorphic-ws';
import WsStore, {
BitgetInstType,
WsTopicSubscribeEventArgs,
} from './util/WsStore';
import WsStore from './util/WsStore';
import {
BitgetInstType,
WebsocketClientOptions,
WSClientConfigurableOptions,
WsKey,
WsTopic,
WsTopicSubscribeEventArgs,
} from './types';
import {
isWsPong,
WsConnectionStateEnum,
WS_AUTH_ON_CONNECT_KEYS,
WS_KEY_MAP,
DefaultLogger,
@@ -26,6 +24,7 @@ import {
isPrivateChannel,
getWsAuthSignature,
} from './util';
import { WsConnectionStateEnum } from './util/WsStore.types';
const LOGGER_CATEGORY = { category: 'bitget-ws' };
@@ -70,10 +69,13 @@ export declare interface WebsocketClient {
): boolean;
}
/**
* @deprecated use WebsocketClientV2 instead
*/
export class WebsocketClient extends EventEmitter {
private logger: typeof DefaultLogger;
private options: WebsocketClientOptions;
private wsStore: WsStore<WsKey>;
private wsStore: WsStore<WsKey, WsTopicSubscribeEventArgs>;
constructor(
options: WSClientConfigurableOptions,
@@ -143,7 +145,6 @@ export class WebsocketClient extends EventEmitter {
}
});
}
/**
* Unsubscribe from topics & remove them from memory. They won't be re-subscribed to if the connection reconnects.
* @param wsTopics topic or list of topics
@@ -646,11 +647,9 @@ export class WebsocketClient extends EventEmitter {
case WS_KEY_MAP.mixv1: {
return WS_BASE_URL_MAP.mixv1.all[networkKey];
}
case WS_KEY_MAP.v2Private: {
return WS_BASE_URL_MAP.v2Private.all[networkKey];
}
case WS_KEY_MAP.v2Private:
case WS_KEY_MAP.v2Public: {
return WS_BASE_URL_MAP.v2Public.all[networkKey];
throw new Error(`Use the WebsocketClientV2 for V2 websockets`);
}
default: {
this.logger.error('getWsUrl(): Unhandled wsKey: ', {
@@ -667,6 +666,8 @@ export class WebsocketClient extends EventEmitter {
* @param instType instrument type (refer to API docs).
* @param topic topic name (e.g. "ticker").
* @param instId instrument ID (e.g. "BTCUSDT"). Use "default" for private topics.
*
* @deprecated use WebsocketClientV2 instead
*/
public subscribeTopic(
instType: BitgetInstType,
@@ -685,6 +686,8 @@ export class WebsocketClient extends EventEmitter {
* @param instType instrument type (refer to API docs).
* @param topic topic name (e.g. "ticker").
* @param instId instrument ID (e.g. "BTCUSDT"). Use "default" for private topics to get all symbols.
*
* @deprecated use WebsocketClientV2 instead
*/
public unsubscribeTopic(
instType: BitgetInstType,