new JsonRpcClientProtocol(factory, version, delimiter)
JsonRpcClientProtocol contructor
Parameters:
| Name | Type | Description |
|---|---|---|
factory |
class | Instance of JsonRpcClientFactory |
version |
1 | 2 | JSON-RPC version to make requests with |
delimiter |
string | Delimiter to use for message buffer |
- Source:
Properties:
| Name | Type | Description |
|---|---|---|
factory |
class | Instance of JsonRpcClientFactory |
connector |
class | The socket instance for the client |
version |
1 | 2 | JSON-RPC version to use |
delimiter |
string | Delimiter to use for message buffer |
message_id |
number | Current message ID |
serving_message_id |
number | Current message ID. Used for external functions to hook into |
pendingCalls |
Object | Key value pairs for pending message IDs to promise resolve/reject objects |
responseQueue |
Object.<(string|number), JSON> | Key value pairs for outstanding message IDs to response object |
server |
Object | Server host and port object {host: "x.x.x.x", port: xxxx} |
messageBuffer |
class | Instance of MessageBuffer |
Methods
-
batch(requests)
-
Used to send a batch request to the server.
Recommend using message to construct the message objects.
Will use the IDs for the requests in the batch in an array as the keys for
pendingCalls.How a client should associate batch responses to their requests is not in the spec, so this is the solution.
Parameters:
Name Type Description requestsArray An array of valid JSON-RPC message objects
- Source:
Returns:
Promise
Example
1
client.batch([client.message("foo", ["bar"]), client.message("hello", [], false)]) -
connect()
-
Make the connection to the server.
Calls setConnector to establish the client connection.
Calls listen if connection was successful, and will resolve the promise.
Will retry connection on the
connectionTimeoutinterval. Number of connection retries is based onremainingRetries.If
nullis set for number of retries, then connections will attempt indefinitely.Will reject the promise if connect or re-connect attempts fail and there are no remaining retries.
- Source:
Returns:
Promise
-
end(cb)
-
Ends connection to the server.
Sets
JsonRpcClientFactory.pcolInstancetoundefinedClears the connection timeout
Parameters:
Name Type Description cbfunction Called when connection is sucessfully closed
- Source:
-
getBatchResponse(batch)
-
Returns the batch response.
Overwrite if class needs to reformat response in anyway (i.e. in HttpClientProtocol)
Parameters:
Name Type Description batchArray Array of valid JSON-RPC message objects
- Source:
-
getResponse(id)
-
Get the outstanding request object for the given ID.
Parameters:
Name Type Description idstring | number ID of outstanding request
- Source:
-
gotBatch(message)
-
Called when the received message is a batch.
Calls gotNotification for every notification in the batch.
Calls gotBatchResponse otherwise.
Parameters:
Name Type Description messageArray.<JSON> A valid JSON-RPC batch message
- Source:
-
gotBatchResponse(batch)
-
Associate the ids in the batch message to their corresponding
pendingCalls.Will call _resolveOrRejectBatch when object is determined.
Parameters:
Name Type Description batchArray Array of valid JSON-RPC message objects
- Source:
-
gotError(error)
-
Calls rejectPendingCalls with error object.
If the object cannot be parsed, then an unkown error code is sent with a
nullid.Parameters:
Name Type Description errorstring Stringified JSON-RPC error object
- Source:
-
gotNotification(message)
-
Called when the received
messageis a notification. Emits an event usingmessage.methodas the name. The data passed to the event is themessage.Parameters:
Name Type Description messageJSON A valid JSON-RPC message object
- Source:
-
gotResponse(message)
-
Called when the received message is a response object from the server.
Gets the response using getResponse.
Resolves the message and removes it from the
responseQueue. Cleans up any outstanding timers.Parameters:
Name Type Description messageJSON A valid JSON-RPC message object
- Source:
-
listen()
-
Setup
this.listner.on("data")event to listen for data coming into the client.Pushes received data into
messageBufferand calls _waitForData- Source:
-
message(method, params [, id])
-
Generate a stringified JSON-RPC message object.
Parameters:
Name Type Argument Default Description methodstring Name of the method to use in the request
paramsArray | JSON Params to send
idboolean <optional>
true If true it will use instances
message_idfor the request id, if false will generate a notification request- Source:
Example
-
notify(method, params)
-
Send a notification to the server.
Promise will resolve if the request was sucessfully sent, and reject if there was an error sending the request.
Parameters:
Name Type Description methodstring Name of the method to use in the notification
paramsArray | JSON Params to send
- Source:
Returns:
Promise
Example
1
client.notify("hello", ["world"]) -
rejectPendingCalls(error)
-
Reject the pending call for the given ID is in the error object.
If the error object has a null id, then log the message to the logging .getLogger().
Parameters:
Name Type Description errorstring Stringified JSON-RPC error object
- Source:
-
request()
-
- Source:
Returns:
Object
Example
-
send(method, params)
-
Send a request to the server
Promise will resolve when a response has been received for the request.
Promise will reject if the server responds with an error object, or if the response is not received within the set
requestTimeoutParameters:
Name Type Description methodstring Name of the method to use in the request
paramsArray | JSON Params to send
- Source:
Returns:
Promise
Example
1
client.send("hello", {"foo": "bar"}) -
<abstract> setConnector()
-
Set the
connectorattribute for the protocol instance. The connector is essentially the socket or connection instance for the client.- Source:
-
verifyData(chunk)
-
Verify the incoming data returned from the
messageBufferThrow an error if its not a valid JSON-RPC object.
Call gotNotification if the message a notification.
Call gotBatch if the message is a batch request.
Parameters:
Name Type Description chunkstring Data to verify
- Source:
-
write(request [, cb])
-
Send a message to the server
Parameters:
Name Type Argument Description requeststring Stringified JSON-RPC message object
cbfunction <optional>
Callback function to be called when message has been sent
- Source: