UNPKG

@selfcommunity/utils

Version:

Utilities to integrate a Community.

177 lines (176 loc) 3.87 kB
/** * WSClientPropTypes interface */ export interface WSClientPropTypes { /** * The Websocket URI */ uri: string; /** * The Websocket Protocols */ protocols: string | string[]; /** * String to identify the heartbeat message. */ heartbeatMsg?: string; /** * Reconnect the websocket if close */ mustReconnect?: boolean; /** * Callback called after the websocket is connected. */ connected?: () => any; /** * Callback called when the websocket is connecting. */ connecting?: () => any; /** * Callback called after the websocket is disconnected. */ disconnected?: (evt: any) => any; /** * Callback called when a message is received from the websocket. */ receiveMessage?: (data: any) => any; /** * Enable/disable debug */ debug?: boolean; } export interface WSClientType { /** * Send message * @param message */ sendMessage: (message: any) => void; /** * Get current state */ getState: () => string; /** * Get current connection state */ isConnecting: () => boolean; /** * Check if ws is connected */ isConnected: () => boolean; /** * Check if ws is closing connection */ isClosing: () => boolean; /** * Return if ws is closed */ isClosed: () => boolean; /** * Close the connection */ close: () => void; } /** * WSClient: manage socket connection * @param options * @constructor */ export default class WSClient implements WSClientType { private static _instance; private _cfg; private _ws; private _timer; private _attempts; private _heartbeatInterval; private _missedHeartbeats; /** * Constructor * @param cfg */ constructor(cfg: WSClientPropTypes); /** * Get instance */ static getInstance(cfg: any): WSClient; /** * Connect */ connect(): void; /** * Validate options * @param cfg */ isValidOptions(cfg: WSClientPropTypes): boolean; /** * Try to reconnect if previous connection failed * Generate an interval, after that try to reconnect */ tryToReconnect(): void; /** * Reestablish the connection * Increase the number of attempts */ reconnect(): void; /** * Send heartbeat every 5 seconds * If missing more than 3 heartbeats close connection */ sendHeartbeat(): void; /** * Established the new connection * Reset this._attempts counter */ onOpen(): void; /** * Connection closed. Try to reconnect. * @param evt */ onClose(evt: any): void; /** * An error occured * @param evt */ onError(evt: any): void; /** * A message has arrived. * If it is the heartbeat -> reset this._missedHeartbeats * If it is data pass data to the callback * @param evt */ onMessage(evt: any): any; /** * Generate an interval that is randomly between 0 and 2^k - 1, where k is * the number of connection attmpts, with a maximum interval of 30 seconds, * so it starts at 0 - 1 seconds and maxes out at 0 - 30 seconds */ generateInterval(k: any): number; /** * Send message * @param message */ sendMessage(message: any): void; /** * Get the ws state */ getState(): any; /** * Check if ws is in connecting state */ isConnecting(): boolean; /** * Check if ws is connected */ isConnected(): boolean; /** * Check if ws is in closing state */ isClosing(): boolean; /** * Check if ws is closed */ isClosed(): boolean; /** * Close the connection */ close(): void; }