Class: HttpServerFactory

HttpServerFactory

Creates instance of HttpServerFactory


new HttpServerFactory()

In addition to the params and properties for JsonRpcServerFactory the WsServerProtocol has the following properties:

Properties:
Name Type Description
scheme 'http' | 'https'

The scheme to allow connections with

sslOptions object

Any of the ssl options for the http server according to https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options

Source:

Extends

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

Inherited From:
Overrides:
Source:
Returns:

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

Type
* | error

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

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

clientConnected(client)

Called when the server receives a connection event.

Parameters:
Name Type Description
client net.Socket

Instance of net.Socket

Overrides:
Source:
Returns:

Returns an instance of net.Socket

Type
net.Socket

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

Inherited From:
Overrides:
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.

Inherited From:
Overrides:
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.

Inherited From:
Overrides:
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

Inherited From:
Overrides:
Source:

notify(notifications)

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

Array of notifications

Inherited From:
Overrides:
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

Inherited From:
Overrides:
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

Inherited From:
Overrides:
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

Inherited From:
Overrides:
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

Inherited From:
Overrides:
Source:
Throws:

Will throw an error if client is not defined


setServer()

Set the server property for the server factory

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