UNPKG

mobility-toolbox-js

Version:

Toolbox for JavaScript applications in the domains of mobility and logistics.

148 lines (147 loc) 5.59 kB
import type { RealtimeTrajectoryResponse } from '../types'; export declare interface WebSocketAPIMessageEventData<T> { client_reference: null | number | string; content: T; source: string; timestamp: number; } export declare interface WebSocketAPIParameters { args?: number | string; channel?: string; id?: number | string; } export declare interface WebSocketAPIRequest<T> { cb: WebSocketAPIMessageCallback<T>; errorCb?: EventListener; onErrorCb?: EventListener; onMessageCb: WebSocketAPIMessageEventListener; params: WebSocketAPIParameters; requestString: string; } export declare interface WebSocketAPISubscription<T> { cb: WebSocketAPIMessageCallback<T>; errorCb?: EventListener; onErrorCb?: EventListener; onMessageCb: WebSocketAPIMessageEventListener; params: WebSocketAPIParameters; quiet: boolean; } export type WebSocketAPIBufferMessageEventData = { source: 'buffer'; } & WebSocketAPIMessageEventData<RealtimeTrajectoryResponse[]>; /** * This type represents a function that has been call with each feature returned by the websocket. */ export type WebSocketAPIMessageCallback<T> = (data: WebSocketAPIMessageEventData<T>) => void; export type WebSocketAPIMessageEvent = { data: string; } & Event; export type WebSocketAPIMessageEventListener = (evt: WebSocketAPIMessageEvent) => void; export type WebSocketAPISubscribed = Record<string, boolean>; /** * Class used to facilitate connection to a WebSocketAPI and * also to manage properly messages send to the WebSocketAPI. * This class must not contain any specific implementation. * @private */ declare class WebSocketAPI { closed?: boolean; closing?: boolean; connecting?: boolean; messagesOnOpen: string[]; open?: boolean; requests: WebSocketAPIRequest<unknown>[]; subscribed: WebSocketAPISubscribed; subscriptions: WebSocketAPISubscription<unknown>[]; websocket?: WebSocket; constructor(); /** * Get the websocket request string. * * @param {string} method Request mehtod {GET, SUB}. * @param {WebSocketParameters} params Request parameters. * @param {string} params.channel Channel name * @param {string} [params.args] Request arguments * @param {Number|string} [params.id] Request identifier * @return {string} request string * @private */ static getRequestString(method: string, params?: WebSocketAPIParameters): string; addEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void; /** * Close the websocket definitively. * * @private */ close(): void; /** * (Re)connect the websocket. * * @param {string} url Websocket url. * @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions. * @private */ connect(url: string, onOpen?: () => void): void; defineProperties(): void; /** * Sends a get request to the websocket. * The callback is called only once, when the response is received or when the call returns an error. * * @param {Object} params Parameters for the websocket get request * @param {function} cb callback on message event * @param {function} errorCb Callback on error and close event * @private */ get<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>, errorCb?: EventListener): void; /** * Listen to websocket messages. * * @param {WebSocketParameters} params Parameters for the websocket get request * @param {function} cb callback on listen * @param {function} errorCb Callback on error * @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks * @private */ listen<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>, errorCb?: EventListener): { onErrorCb?: EventListener; onMessageCb: WebSocketAPIMessageEventListener; }; removeEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void; /** * Sends a message to the websocket. * * @param {message} message Message to send. * @private */ send(message: string): void; /** * Subscribe to a given channel. * * @param {Object} params Parameters for the websocket get request * @param {function} cb callback on listen * @param {function} errorCb Callback on error * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered. * @private */ subscribe<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>, errorCb?: EventListener, quiet?: boolean): void; /** * After an auto reconnection we need to re-subscribe to the channels. */ subscribePreviousSubscriptions(): void; /** * Unlisten websocket messages. * * @param {Object} params Parameters for the websocket get request. * @param {function} cb Callback used when listen. * @private */ unlisten<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>): void; /** * Unsubscribe from a channel. * @param {string} source source to unsubscribe from * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed. * @private */ unsubscribe<T>(source: string, cb?: WebSocketAPIMessageCallback<T>): void; } export default WebSocketAPI;