UNPKG

@akala/json-rpc-ws

Version:

json-rpc websocket transport

57 lines (56 loc) 2.19 kB
import { Base } from './base.js'; import { type PayloadDataType, Connection, Payload } from './shared-connection.js'; import { type Error as MyError } from './errors.js'; import { SocketProtocolAdapter, type SocketAdapter } from '@akala/core'; /** * json-rpc-ws connection * * @constructor * @param {Socket} socket - web socket for this connection * @param {Object} parent - parent that controls this connection */ export declare class JsonNDRpcSocketAdapter<T> extends SocketProtocolAdapter<T> implements SocketAdapter<T> { constructor(socket: SocketAdapter); } export default abstract class Client<TStreamable, TConnectOptions> extends Base<TStreamable> { private socketConstructor; private options?; constructor(socketConstructor: (address: string, options?: TConnectOptions) => SocketAdapter<Payload<TStreamable>>, options?: TConnectOptions); socket?: SocketAdapter<Payload<TStreamable>>; /** * Connect to a json-rpc-ws server * * @param {String} address - url to connect to i.e. `ws://foo.com/`. * @param {function} callback - optional callback to call once socket is connected * @public */ connect(address: string, callback: (err?: Event) => void): void; /** * Test whether we have a connection or not * * @returns {Boolean} whether or not we have a connection * @public */ isConnected(): boolean; /** * Return the current connection (there can be only one) * * @returns {Object} current connection * @public */ getConnection(): Connection<TStreamable>; /** * Close the current connection */ disconnect(): Promise<CloseEvent>; /** * Send a method request * * @param {String} method - name of method * @param {Array} params - optional parameters for method * @param {function} callback - optional reply handler * @public * @todo allow for empty params aka arguments.length === 2 */ send<TParamType extends PayloadDataType<TStreamable>, TReplyType extends PayloadDataType<TStreamable>>(method: string, params: TParamType, callback?: (error?: MyError, result?: TReplyType) => void): void; }