socket-actions
Version:
Websocket implementation to simplify communication and queueing of user actions.
55 lines (54 loc) • 1.88 kB
TypeScript
export type MessageObject = MessageEvent & {
requestId?: string;
};
export type onOpen = () => Promise<void> | void;
export type onClose = () => Promise<void> | void;
export type messageReceiver = (message: MessageObject) => Promise<void> | void;
export type clientOptions = {
url?: string;
authentication?: any;
connectionTryLimit?: number;
secondsBetweenRetries?: number;
protocols?: string | string[];
onOpen?: onOpen;
onClose?: onClose;
onMessage?: messageReceiver;
onAuthResponse?: messageReceiver;
onAuthFailure?: messageReceiver;
};
export default class Client {
private _authentication;
private _socket;
private readonly url;
private readonly protocols;
private readonly preparedOnAuthResponse;
private readonly preparedOnMessageResponse;
private readonly onMessage;
private readonly onAuthResponse;
private readonly onOpen;
private readonly onClose;
private readonly onAuthFailure;
private connectionTries;
private readonly connectionTryLimit;
private readonly secondsBetweenRetries;
private _isAuthenticated;
private _isConnected;
private requests;
constructor(options?: clientOptions);
private connect;
reconnect(): void;
private opening;
private closing;
close(code?: number | undefined, reason?: string | undefined): void;
private enableMessageReceiver;
private disableMessageReceiver;
private authResponse;
private messageResponse;
get authentication(): any;
get isAuthenticated(): boolean;
get isConnected(): boolean;
get socket(): WebSocket | null;
tryAuth(authentication?: any): void;
sendAction(path: string, data?: Record<string, any>, extraDetails?: Record<string, any>): void;
sendRequest(path: string, data?: Record<string, any>, timeout?: number): Promise<Record<string, any>>;
}