UNPKG

@itick/browser-sdk

Version:

Official iTick API SDK for browser. Real-time & historical data for global Stocks, Forex, Crypto, Indices, Futures, Funds, Precious Metals. REST (OHLCV/K-line) + low-latency WebSocket. Promise-based, TypeScript-ready. For quant trading & fintech

96 lines 3.36 kB
import { OnMessageHandler, ErrorHandler, OpenHandler, CloseHandler, MessageOption, WsOptions, SubscribeData } from '../types/socket'; export default class SocketClient { protected socket: WebSocket | null; protected wssURL: string; protected wsPath: string; protected token: string; protected isRunning: boolean; protected reconnectAttempts: number; protected pingInterval: NodeJS.Timeout | null; protected reconnectTimeout: NodeJS.Timeout | null; protected PING_INTERVAL: number; protected RECONNECT_INTERVAL: number; protected MAX_RECONNECT_ATTEMPTS: number; protected messageHandlers: OnMessageHandler[]; protected errorHandlers: ErrorHandler[]; protected openHandlers: OpenHandler[]; protected closeHandlers: CloseHandler[]; protected lastSubscription: SubscribeData | null; constructor(token: string, options: WsOptions); /** * WebSocket Message Handler */ onSocketMessage(handler: OnMessageHandler): void; /** * WebSocket Error Handler */ onSocketError(handler: ErrorHandler): void; /** * WebSocket Connection Open Handler */ onSocketOpen(handler: OpenHandler): void; /** * WebSocket Connection Close Handler */ onSocketClose(handler: CloseHandler): void; /** * Internal WebSocket Connection Method */ private _connectWebSocket; /** * Start heartbeat ping */ protected _startPing(): void; /** * Stop heartbeat ping */ protected _stopPing(): void; /** * Schedule reconnection */ protected _scheduleReconnect(): void; /** * WebSocket Subscription Method * @description * message object must contain ac, types and params fields * - ac: operation type, must be "subscribe" or "unsubscribe" * - types: subscription types, can be comma-separated string or string array, supports "quote", "depth", "tick", "kline@1m" etc. kline@1m can also be written as kline@1, SDK will automatically convert to correct format * - params: subscription parameters, can be comma-separated string or string array, format is "code$region" (e.g., "AAPL$US") SDK will automatically handle formatting * - String array format is recommended for clearer code and fewer errors * @example * Example 1: Subscribe to AAPL and TSA real-time quotes/order book/trades/1-minute K-line data * ```json * { * "ac": "subscribe", * "types": "quote,depth,tick,kline@1", * "params": "AAPL$US,TSA$US" * } * ``` * Example 2: Subscribe to AAPL and TSA real-time quotes/order book/trades/1-minute K-line data * ```json * { * "ac": "subscribe", * "types": ["quote","depth","tick","kline@1m"], * "params": ["AAPL$US","TSA$US"] * } * ``` * * Both methods can correctly subscribe, SDK will automatically handle type conversion and parameter formatting * */ subscribeSocket(data: MessageOption): void; /** * Restore subscription after reconnection */ protected _resubscribeLast(): void; /** * Close WebSocket */ closeWebSocket(): void; /** * Check if WebSocket is connected */ checkSocketConnected(): boolean; disconnectSocket(): void; } //# sourceMappingURL=SocketClient.d.ts.map