UNPKG

wspromisify

Version:

Wraps your WebSockets into Promise-based class with full d.ts typings on client & server

84 lines (80 loc) 2.31 kB
// Generated by dts-bundle-generator v9.5.1 declare namespace wsc { interface DataObject { [key: string]: any; } type WSEvent = "open" | "message" | "close" | "error" | "timeout"; /** Minimal socket-like interface. */ interface Socket { readyState: number; send(...any: any[]): void; close(): void; addEventListener(event: string, handler: ((event: any) => any), ...any: any[]): void; } type AsyncErrCode = Promise<number | null | {}>; type EventHandler = (e: any) => void; type DataPipe = (message: any) => any; type DataType = "json" | "string"; interface Config { data_type: DataType; log(event: string, time?: number | null, message?: any): void; log(event: string, message?: any): void; timer: boolean; url: string; timeout: number; reconnect: number; lazy: boolean; socket: Socket | null; adapter: (host: string, protocols?: string[]) => Socket; encode: (key: string, message: any, config: Config) => any; decode: (rawMessage: any) => { [id_or_data_key: string]: string; }; protocols: string[]; pipes: DataPipe[]; server: { id_key: string; data_key: string; }; ping: { interval: number; content: any; }; } type UserConfig = Partial<Config>; interface SendOptions { top: any; data_type: DataType; } } declare class WebSocketClient { private open; private ws; private forcibly_closed; private reconnect_timeout; private queue; private messages; private onReadyQueue; private onCloseQueue; private handlers; private config; private init_flush; private call; private log; private initSocket; private connect; get socket(): wsc.Socket | null; ready(): Promise<void>; on(event_name: wsc.WSEvent, handler: (data: any) => any, predicate?: (data: any) => boolean, raw?: boolean): number | void; close(): wsc.AsyncErrCode; /** .send(your_data) wraps request to server with {id: `hash`, data: `actually your data`}, returns a Promise that will be rejected after a timeout or resolved if server returns the same signature: {id: `same_hash`, data: `response data`}. */ send<RequestDataType = any, ResponseDataType = any>(message_data: RequestDataType, opts?: wsc.SendOptions): Promise<ResponseDataType>; constructor(user_config?: wsc.UserConfig); } export { WebSocketClient as default, }; export {};