UNPKG

tgrid

Version:

Grid Computing Framework for TypeScript

118 lines (117 loc) 4.45 kB
import { Invoke } from "../../components/Invoke"; import { ConnectorBase } from "../internal/ConnectorBase"; import { IWebSocketCommunicator } from "./internal/IWebSocketCommunicator"; /** * Web Socket Connector. * * The `WebSocketConnector` is a communicator class which connects to a * {@link WebSocketServer websocket server}, and interacts with it through RPC * (Remote Procedure Call) concept. * * You can connect to the websocket server using {@link connect} method. The * interaction would be started if the server accepts your connection by calling * the {@link WebSocketAcceptor.accept} method. If the remote server rejects your * connection through {@link WebSocketAcceptor.reject} method, the exception * would be thrown. * * After the connection, don't forget to {@link closing} the connection, if your * business logics have been completed, to clean up the resources. Otherwise, the * closing must be performed by the remote websocket server, you can wait the * remote server's closing signal through the {@link join} method. * * Also, when declaring this `WebSocketConnector` type, you've to define three * generic arguments; `Header`, `Provider` and `Remote`. Those generic arguments must * be same with the ones defined in the target {@link WebSocketServer} and * {@link WebSocketAcceptor} classes (`Provider` and `Remote` must be reversed). * * For reference, the first `Header` type represents an initial data from the * remote client after the connection. I recommend utilize it as an activation tool * for security enhancement. The second generic argument `Provider` represents a * provider from client to server, and the other `Remote` means a provider from the * remote server to client. * * @template Header Type of the header containing initial data. * @template Provider Type of features provided for the remote server. * @template Remote Type of features supported by remote server. * @author Jeongho Nam - https://github.com/samchon */ export declare class WebSocketConnector<Header, Provider extends object | null, Remote extends object | null> extends ConnectorBase<Header, Provider, Remote> implements IWebSocketCommunicator { /** * @hidden */ private socket_?; /** * Connect to remote websocket server. * * Try connection to the remote websocket server with its address and waiting for the * server to accept the trial. If the server rejects your connection, then exception * would be thrown (in *Promise.catch*, as `WebSocketError`). * * After the connection and your business has been completed, don't forget to closing the * connection in time to prevent waste of the server resource. * * @param url URL address to connect. * @param options Detailed options like timeout. */ connect(url: string, options?: Partial<WebSocketConnector.IConnectOptions>): Promise<void>; /** * @hidden */ private _Wait_connection; /** * @inheritDoc */ close(code?: number, reason?: string): Promise<void>; /** * @hidden */ private _Handshake; /** * Connection URL. */ get url(): string | undefined; /** * Get state. * * Get current state of connection state with the websocket server. * * List of values are such like below: * * - `NONE`: The {@link WebSocketConnector} instance is newly created, but did nothing yet. * - `CONNECTING`: The {@link WebSocketConnector.connect} method is on running. * - `OPEN`: The connection is online. * - `CLOSING`: The {@link WebSocketConnector.close} method is on running. * - `CLOSED`: The connection is offline. */ get state(): WebSocketConnector.State; /** * @hidden */ protected sendData(invoke: Invoke): Promise<void>; /** * @hidden */ private _Handle_message; /** * @hidden */ private _Handle_close; } /** * */ export declare namespace WebSocketConnector { /** * Current state of the {@link WebSocketConnector}. */ export import State = ConnectorBase.State; /** * Connection options for the {@link WebSocketConnector.connect}. */ interface IConnectOptions { /** * Milliseconds to wait the web-socket server to accept or reject it. If omitted, the waiting would be forever. */ timeout: number; } }