@dxfeed/dxlink-websocket-client
Version:
dxLink WebSocket Client allows to connect the remote dxLink WebSocket endpoint
71 lines (70 loc) • 2.66 kB
TypeScript
import { type DXLinkWebSocketMessage } from './messages';
/**
* Interface for a WebSocket connector that manages the connection to a WebSocket server.
* It provides methods to start and stop the connection, send messages, and set listeners for open, close, and message events.
*/
export interface DXLinkWebSocketConnector {
/**
* Returns the URL of the WebSocket connection.
*/
getUrl(): string;
/**
* Starts the WebSocket connection.
*/
start(): void;
/**
* Stops the WebSocket connection and cleans up resources.
*/
stop(): void;
/**
* Sends a message over the WebSocket connection.
* @param message The message to send.
*/
sendMessage(message: DXLinkWebSocketMessage): void;
/**
* Sets a listener that is called when the WebSocket connection is opened.
* @param listener The listener function to call when the connection is opened.
*/
setOpenListener(listener: () => void): void;
/**
* Sets a listener that is called when the WebSocket connection is closed.
* @param listener The listener function to call when the connection is closed.
*/
setCloseListener(listener: DXLinkWebSocketCloseListener): void;
/**
* Sets a listener that is called when a message is received from the WebSocket server.
* @param listener The listener function to call when a message is received.
*/
setMessageListener(listener: (message: DXLinkWebSocketMessage) => void): void;
}
/**
* Type for a close listener that is called when the WebSocket connection is closed.
* @param reason - The reason for the closure.
* @param error - Indicates if the closure was due to an error.
*/
export type DXLinkWebSocketCloseListener = (reason: string, error: boolean) => void;
/**
* Default connector for the WebSocket connection.
* @internal
*/
export declare class DefaultDXLinkWebSocketConnector implements DXLinkWebSocketConnector {
private readonly url;
private readonly protocols?;
private socket;
private isAvailable;
private openListener;
private closeListener;
private messageListener;
constructor(url: string, protocols?: string | string[] | undefined);
start(): void;
stop(): void;
sendMessage: (message: DXLinkWebSocketMessage) => void;
setOpenListener: (listener: () => void) => void;
setCloseListener: (listener: DXLinkWebSocketCloseListener) => void;
setMessageListener: (listener: (message: DXLinkWebSocketMessage) => void) => void;
getUrl: () => string;
private handleOpen;
private handleClosed;
private handleError;
private handleMessage;
}