UNPKG

@pecometer/pecots-websocket-client

Version:

PecoTS Framework Websocket Client

94 lines (93 loc) 3.41 kB
import { Observable } from 'rxjs'; /** * A websocket client for use with the PecoTS Framework websockets * Once initialised it will connect and maintain the websocket connection * according to the settings sent by the server. If enabled on the backend you may perform HTTP requests * over the websocket which will mimic the standard Http request process. * @example * const ws = new PecoTsWebSocket(); * const url = 'ws:...'; * const jwt = 'your user auth token from the login process'; * ws.init(url,jwt); * const listener = ws.register('an-event-from-the-server').subscribe(data=>{ * // handle the event * ... * }); * listener.unsubscribe(); */ export declare class PecoTsWebSocket { private _listeners; private _endPoint; private _socket; private _pingTimeout; private readonly _timeout; private _pongTimeout; private _isConnected; private _connectionID; private _jwtToken; private _connectionAttempts; private _config; /** * Returns a boolean indicating the connection status * @returns {boolean} The current connection status */ get connected(): boolean; /** * Returns the connection ID as set by the server * @returns {string} The connection ID as a string */ get connectionID(): string; /** * Initialises the websocket connection, setting up the required event listeners * @param {string} url The fully qualified domain, including protocol, that the socket should attempt to connect to * @param {string} jwt The JWT from the user authentication process that the connection should authenticate itself with */ init(url: string, jwt: string): void; /** * Disconnects the websocket connection */ disconnect(): void; /** * Registers a new listener against the provided event, all listeners should be deregistered by called the * `unsubscribe()` function of the returned observable * * @example * const sub = ws.register('event').... * ... * sub.unsubscribe(); * * @param {string} event The event that the listener should be called on * @returns {Observable<any>} Returns an observable that will emit the event data once an event is triggered */ register(event: string): Observable<unknown>; /** * Setups a timeout that will trigger a PING message to confirm the socket is still alive * Once initiated a new timeout will be set giving the responding PONG message a reply limit */ private _ping; /** * Broadcasts the recieved message to all listeners that have registered for the event * @param {any} data The message data, containing the event and the related data */ private _broadcast; /** * Sends a message to the WebSocket server * @param {string} message The message to send to the server */ private _sendMessage; /** * Returns a close handler as a function * Useful so we can remove the event if needed */ private _closeHandler; /** * Handles the message received from the server * @param {string} message The message received from the server */ private _handleMessage; /** * Handles the initial connection message from the server * @param {string} messageData The message received from the server */ private _handleInitialConnectionMessage; }