import { Shell } from "../Shell"; /** * RPC over websocket, using the JSON-RPC 2.0 Specification * http://www.jsonrpc.org/specification * * A `Cable` establish an RPC communication over a websocket, there is no server/client role, the same cable can * you can define methods on both sides of the cable and call the remote methods * * @example * ```typescript * * // Client 1 * const cable = new Cable(ws); * cable.register("ping", async () => { * return "pong"; * }); * cable.notify("hello", {name:"client 1"}); * * // Client 2 * const cable = new Cable(ws); * cable.register("hello", async ({name:string}) => { * console.log(`${name} said hello`); * }); * try { * const res = await cable.request("ping"); * assert.equal(res,"pong"); * } catch(e) { * if(e.code === Cable.SERVER_ERROR) { * console.log("Implementation error on the server"); * } * throw e; * } * ``` */ export declare class Cable extends Shell { /** * Invalid JSON was received by the server. * An error occurred on the server while parsing the JSON text. */ static readonly PARSE_ERROR: number; /** * The JSON sent is not a valid Request object. */ static readonly INVALID_REQUEST: number; /** * The method does not exist / is not available. */ static readonly METHOD_NOT_FOUND: number; /** * Invalid method parameter(s). */ static readonly INVALID_PARAMS: number; /** * Internal JSON-RPC error. */ static readonly INTERNAL_ERROR: number; /** * Generic server-errors */ static readonly SERVER_ERROR: number; private static readonly id; private static index; private calls; private methods; /** * @param ws An object compatible with the WebSocket interface. */ constructor(ws: WebSocket); /** * Register a new method on the websocket * @param {string} method name * @param {(params: any) => Promise} method handler */ register(name: string, method: (params: any) => Promise): void; /** * Make a Rpc call * @param {string} method name * @param {object | any[]} params * @param {number} if set will reject if no response is received after `timeout` ms * @returns {Promise} */ request(method: string, params?: object | any[], timeout?: number): Promise<{}>; /** * Unlike the _request_, notify will not wait for the server to reply * @param {string} method name * @param {object | any[]} params * @returns {Promise} */ notify(method: string, params?: object | any[]): void; private guardParameters(params?); private timeout(id); private sendMessage(id, message); private sendError(id, code, message); private parseMessage(message); private receivedMessage(message); private rpcCall(id, method, params); private rpcResult(id, results); private rpcError(id, code, message); }