/// import { EventEmitter } from 'events'; import { PushChannel, WsFailureResponse, WsPush, WsSubscribeResponse, WsSubscriptionTopic, WsTradeBaseRequest, WsTradeResponse } from './type'; import { WsKey, WsMarket } from './ws-util'; /** * OkxWsClientOptions */ export interface Options { market: WsMarket; /** * set apiKey, passphrase, and secretKey, it will login to channels that need authorization, otherwise it won't init private channel. */ apiKey?: string; /** * set, it will login to channels that need authorization */ passphrase?: string; /** * set, it will login to channels that need authorization */ secretKey?: string; /** * in ms, if not set, it will use default value 3000 */ heartbeatInterval?: number; } export type LogType = 'send' | 'login' | 'errorResponse' | 'subscribe' | 'trade' | 'ping' | 'pong' | PushChannel; export type LogSettings = Partial>; export declare const WS_KEYS: WsKey[]; /** * declare overrote methods of OkxWebSocketClient */ export declare interface OkxWebSocketClient { emit(event: 'login', wsKey: string): boolean; emit(event: 'errorResponse', data: WsFailureResponse): boolean; emit(event: 'subscribe', data: WsSubscribeResponse): boolean; emit(event: 'trade', data: WsTradeResponse): boolean; emit(event: PushChannel, data: WsPush): boolean; emit(event: 'open', wsKey: string): boolean; on(event: 'login', listener: (wsKey: WsKey) => void): this; on(event: 'errorResponse', listener: (data: WsFailureResponse) => void): this; on(event: 'subscribe', listener: (data: WsSubscribeResponse) => void): this; on(event: 'trade', listerner: (data: WsTradeResponse) => void): this; on(event: PushChannel, listener: (data: WsPush) => void): this; on(event: 'open', listener: (wsKey: WsKey) => void): this; } export declare class OkxWebSocketClient extends EventEmitter { /** * WebSocket clients for different channels */ private _clients; private _apiKey; private _passphrase; private _secretKey; private _timerId; private _activeClients; private _privateChannelReady; /** * Type of ws endpoint */ private _market; /** * subcriptions, when reconnect need to re-subscribe */ private _subscribes; /** * Timer for last activities */ private _pingPongTimer; private _heartbeatInterval; private _verbose; constructor(options: Options); /** * Init WebSocket Client for certain Channel * @param key {WebSocketChannelKey} * @returns {WebSocket} */ private _initClient; private _sendPing; /** * log for last activities * @param wsKey WS client * @param isReceived whether is receiving message, otherwise is sending message */ private _logTimer; private _needToLogin; /** * Login to private channel */ private _loginToWsClient; /** * Handle message received from a specific WS sockect endpoint * @param key {WebSocketChannelKey} Key of the channel * @param message Message content */ private _onMessage; private _handleFailureResponse; private _handleLoginResponse; private _handleSubscribeResponse; private _handleTradeResponse; private _handlePush; /** * Send message to certain WS endpoint * @param wsKey Key of the WS endpoint * @param message Message content */ private _send; /** * Subscribe/unsubscribe push for a certain ws endpoint * @param req request args, need to make sure all the args are from the same channel * @param resubWhenReconnect whether to resubscript or not when reconnect */ private _subUnsub; private _checkHeartbeat; /** * re-subscribe all when re-connect worksocked client * @param wsKey */ private _reSubAfterReconnect; /** * Subscribe push from a certain channel * @param req request object */ subscribe(arg: T): void; /** * Unsubscribe push from a certain channel * @param req request object */ unsubscribe(arg: T): void; /** * trigger order actions over websockets (e.g. placing & cancelling orders) * @param req request object */ trade(req: WsTradeBaseRequest): void; get verbose(): LogSettings; set verbose(value: LogSettings); private _log; /** * Wait for availability of private ws endpoing, we can only subscribe and get pushed message after this * @returns Promise */ privateChannelReady(key: 'private' | 'business'): Promise; /** * Release resources */ dispose(): void; /** * unique client, sole client */ private static _instance; static getInstance(opts: Options): OkxWebSocketClient; }