/// import { EventEmitter } from 'events'; import { WsSubscriptionTopic } from './type/meta'; import { PushChannel, WsPush } from './type/push'; import { WsSubRequest, WsTradeBaseRequest, WsUnsubRequest } from './type/request'; import { WsFailureResponse, WsSubscribeResponse, WsTradeResponse } from './type/response'; import { MARKET, WS_KEY } from './ws-util'; /** * OkxWsClientOptions */ export interface Options { market: MARKET; apiKey: string; passphrase: string; secretKey: string; heartbeatInterval?: number; } /** * 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; on(event: 'login', listener: (wsKey: WS_KEY) => void): this; on(event: 'errorResponse', listener: (data: WsFailureResponse) => void): this; on(event: 'subscribe', listeneer: (data: WsSubscribeResponse) => void): this; on(event: 'trade', listerner: (data: WsTradeResponse) => void): this; on(event: PushChannel, listener: (data: WsPush) => void): this; } export declare class OkxWebSocketClient extends EventEmitter { /** * WebSocket clients for different channels */ private _clients; private _apiKey; private _passphrase; private _secretKey; private _timerId; private _privateChannelReady; /** * Type of ws endpoint */ private _market; /** * subcriptions, when reconnect need to re-subscribe */ private _subscribes; /** * Timer for last activities */ private _pingPongTimer; private _verbose; /** * @deprecated use `OkxWebSocket.getInstance` instead to ensure the application will use the same clients all the time. */ 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; /** * 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; /** * Subscribe push from a certain channel * @param req request object */ subscribe(req: WsSubRequest): void; /** * Unsubscribe push from a certain channel * @param req request object */ unsubscribe(req: WsUnsubRequest): void; /** * trigger order actions over websockets (e.g. placing & cancelling orders) * @param req request object */ trade(req: WsTradeBaseRequest): void; get verbose(): boolean; set verbose(value: boolean); /** * Wait for availability of private ws endpoing, we can only subscribe and get pushed message after this * @returns Promise */ privateChannelReady(key: 'private' | 'business'): Promise; /** * unique client, sole client */ private static _instance; static getInstance(opts: Options): OkxWebSocketClient; }