// Type definitions for json-rpc-ws 4.0 // Project: https://github.com/andyet/json-rpc-ws // Definitions by: Nicolas Penin // Margus Lamp // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 import * as ws from 'ws'; export function createServer(): Server; export function createClient(): Client; export class Server extends Base { constructor(); /** * Start the server */ start(options?: ws.ServerOptions, callback?: () => void): void; server: ws.Server; /** * Stop the server */ stop(): void; } export class Client extends Base { constructor(WebSocket: ws, browser?: boolean); /** * Connect to a json-rpc-ws server */ connect(url: string, connected: () => void): void; /** * Test whether we have a connection or not */ isConnected(): boolean; /** * Return the current connection (there can be only one) */ getConnection(): TConnection; /** * Close the current connection */ disconnect(callback: () => void): void; } export class Base { /** * Add a handler function for a given method */ expose(eventName: string, handler: Handler): void; /** * Send a method request through a specific connection */ send(eventName: string, params?: ParamType, callback?: ReplyCallback): void; send(eventName: string, params: ParamType): void; /** * Connected event handler */ connected(socket: ws): void; /** * Disconnected event handler */ disconnected(connection: TConnection): void; /** * Test if a handler exists for a given method */ hasHandler(method: string): boolean; /** * Get handler for a given method */ getHandler(method: string): Handler; /** * Get a connection by id */ getConnection(id: string): Connection; /** * Shut down all existing connections */ hangup(): void; } export interface Connection { id: string; socket: ws; parent: Base; responseHandlers: { [id: string]: ReplyCallback }; sendRaw(payload: Payload): void; processPayload(payload: Payload): void; sendResult(id: string, error?: any, result?: any): void; sendMethod(method: string, params: ParamType, callback?: ReplyCallback): void; sendError(error: any, id?: string, data?: any): void; close(error?: any): void; hangup(callback?: () => void): void; message(data: any): void; } export interface Payload { jsonrpc?: '2.0'; id: string; method: string; params?: ParamType; result?: any; error?: any; } export type Handler = (this: TConnection, params: ParamType, reply: ReplyCallback) => void; export type ReplyCallback = (error: any, params?: ParamType) => void; /** * Returns a valid jsonrpc2.0 error reply */ export function Errors(type: string, id: string | number | null, data: any): any | null;