Class: JsonRpcServerFactory

JsonRpcServerFactory

Creates an instance of JsonRpcServerFactory


new JsonRpcServerFactory(options)

Parameters:
Name Type Description
options object
Properties
Name Type Argument Default Description
host Object <optional>

Host IP to open server with

port Object <optional>

Host port to open server with

version Number <optional>
2

JSON-RPC version to use (1|2)

delimiter String <optional>
"\n"

Delimiter to use for JsonRpcServerProtocol

exlusive Boolean <optional>
false

disallow port sharing

Properties:
Name Type Description
methods object

Key value pairs of server method to function call

clients array

List of client connections which are instances of JsonRpcServerProtocol

listening boolean

Inidicates if the server is currently listening

protocol Object

Instance of JsonRpcServerProtocol to use for client connections

Source:

Extends

  • events

Members


<static> http :HttpServerFactory

HTTP server constructor

Type:
Source:

<static> tcp :TcpServerFactory

TCP server constructor

Type:
Source:

<static> ws :WsServerFactory

WS server constructor

Type:
Source:

Methods


_removeFromArray(item, array)

Removes item from the given list

Parameters:
Name Type Description
item *

Any item in the list

array array

The array to remove the item from

Source:
Returns:

Returns the item that was removed from the array or {error}

Type
* | error

<abstract> buildProtocol()

Establishes the client connection using the protocol instance and adds the newly connected client to this.clients.

Registers the event to call clientDisconnected when a client a closes the connection

Source:
Example
const pcol = JsonRpcServerProtocol()
pcol.clientConnected()
this.clients.push(pcol)

clientConnected(pcol)

Called when client receives a connection event.

Parameters:
Name Type Description
pcol JsonRpcServerProtocol

A JsonRpcServerProtocol instance

Source:
Returns:

Returns a client for a given JsonRpcServerProtocol instance

Type
JsonRpcServerProtocol.client

clientDisconnected(pcol)

Called when client disconnects from server.

If overwriting, its recommended to call JsonRpcServerFactory._removeFromArray manually to ensure this.clients is cleaned up

Calls this._removeFromArray and removes disconnected client from this.clients list

Parameters:
Name Type Description
pcol JsonRpcServerProtocol

A JsonRpcServerProtocol instance

Source:
Returns:

Returns an object of {host, port} for the given protocol instance, or {error} if there was an error retrieving the client

Type
object | error

close()

Closes the server connection and kicks all connected clients.

Sets listening property to false.

Source:
Returns:

Will reject if any error was present

Type
Promise

listen()

Start listening for client connections to server.

Calls setServer and buildProtocol.

Establishes error and close listeners.

Source:
Returns:

Resolves host and port address for server.

Type
Promise

method(name, cb)

Register a method and associated function with the server.

The function will be called when a client makes a request to this method.

Parameters:
Name Type Description
name string

Name of method

cb function

Function to call when client makes request to method

Source:

notify(notifications)

Parameters:
Name Type Description
notifications Array.<Array.<string, (Array|object)>>

Array of notifications

Source:
Returns:

Returns list of error objects if there was an error sending to any client. Returns true if the entire data was sent successfully Returns false if all or part of the data was not sent to the client.

Type
Array.<boolean> | Array.<Error>
Example
server.notify([
   ["hello", ["world"]],
   ["foo", {"bar": "baz"}]
])

onNotify(method, cb)

Call function when notification with event name comes in.

Parameters:
Name Type Description
method string

Method name to listen for notification

cb function

Name of callback function fired when method event comes in

Source:
Example
function world(){
 return 'foo'
}
server.onNotify("hello", world)

removeAllOnNotify(method)

Remove all functions listening for notification.

Parameters:
Name Type Description
method string

Method name to remove events for

Source:

removeOnNotify(method, cb)

Remove function name from listening for notifications.

Parameters:
Name Type Description
method string

Method name to remove

cb function

Name of the callback function to remove

Source:
Example
function world(){
 return 'foo'
}
server.removeOnNotify("hello", world)

sendNotification(client, response)

Send notification to client

Parameters:
Name Type Description
client class

Client instance

response string

Stringified JSON-RPC message to sent to client

Source:
Throws:

Will throw an error if client is not defined


<abstract> setServer()

Set the server property for the server factory

Source:
Example
this.server = new net.Server()