new HttpClientProtocol()
In addition to the params and properties of JsonRpcClientProtocol, the HttpClientProtocol has the following properties
- Source:
Properties:
Name | Type | Description |
---|---|---|
headers |
object | HTTP headers passed to the factory instance |
encoding |
string | Encoding type passed to the factory instance |
scheme |
'http' | 'https' | The scheme to use to connect to the server |
Requires:
- module:http
- module:https
Extends
Requires
- module:http
- module:https
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 requests
Array An array of valid JSON-RPC message objects
- Inherited From:
- Overrides:
- Source:
Returns:
Promise
Example
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
connectionTimeout
interval. Number of connection retries is based onremainingRetries
.If
null
is 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.
- Inherited From:
- Overrides:
- Source:
Returns:
Promise
-
end(cb)
-
Ends connection to the server.
Sets
JsonRpcClientFactory.pcolInstance
toundefined
Clears the connection timeout
Parameters:
Name Type Description cb
function Called when connection is sucessfully closed
- Inherited From:
- Overrides:
- Source:
-
getBatchResponse(batch)
-
Returns the batch response.
Overwrite if class needs to reformat response in anyway (i.e. in HttpClientProtocol)
Parameters:
Name Type Description batch
Array Array of valid JSON-RPC message objects
- Inherited From:
- Overrides:
- Source:
-
getResponse(id)
-
Get the outstanding request object for the given ID.
Parameters:
Name Type Description id
string | number ID of outstanding request
- Inherited From:
- Overrides:
- 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 message
Array.<JSON> A valid JSON-RPC batch message
- Inherited From:
- Overrides:
- 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 batch
Array Array of valid JSON-RPC message objects
- Inherited From:
- Overrides:
- Source:
-
gotError(error)
-
Calls rejectPendingCalls with error object.
If the object cannot be parsed, then an unkown error code is sent with a
null
id.Parameters:
Name Type Description error
string Stringified JSON-RPC error object
- Inherited From:
- Overrides:
- Source:
-
gotNotification(message)
-
Called when the received
message
is a notification. Emits an event usingmessage.method
as the name. The data passed to the event is themessage
.Parameters:
Name Type Description message
JSON A valid JSON-RPC message object
- Inherited From:
- Overrides:
- 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 message
JSON A valid JSON-RPC message object
- Inherited From:
- Overrides:
- Source:
-
listen()
-
Setup
this.listener.on("data")
event to listen for data coming into the client.The HTTP client does not use the delimiter since the completion of a response indicates the end of data.
Calls _waitForData
- Overrides:
- Source:
-
message(method, params [, id])
-
Generate a stringified JSON-RPC message object.
Parameters:
Name Type Argument Default Description method
string Name of the method to use in the request
params
Array | JSON Params to send
id
boolean <optional>
true If true it will use instances
message_id
for the request id, if false will generate a notification request- Inherited From:
- Overrides:
- Source:
Example
client.message("hello", ["world"]) // returns {"jsonrpc": "2.0", "method": "hello", "params": ["world"], "id": 1} client.message("hello", ["world"], false) // returns {"jsonrpc": "2.0", "method": "hello", "params": ["world"]}
-
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. For the HttpClientProtocol, the resolved promise will return the http response object with a
204
response code per the spec.Parameters:
Name Type Description method
string Name of the method to use in the notification
params
Array | JSON Params to send
- Overrides:
- Source:
Returns:
Promise
Example
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 error
string Stringified JSON-RPC error object
- Inherited From:
- Overrides:
- Source:
-
request()
-
- Inherited From:
- Overrides:
- Source:
Returns:
Object
Example
client.request().send("hello", ["world"]) client.request().notify("foo") client.request().message("foo", ["bar"])
-
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
requestTimeout
Parameters:
Name Type Description method
string Name of the method to use in the request
params
Array | JSON Params to send
- Inherited From:
- Overrides:
- Source:
Returns:
Promise
Example
client.send("hello", {"foo": "bar"})
-
<abstract> setConnector()
-
Set the
connector
attribute for the protocol instance. The connector is essentially the socket or connection instance for the client.- Inherited From:
- Overrides:
- Source:
-
verifyData(chunk)
-
Verify the incoming data returned from the
messageBuffer
Throw 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 chunk
string Data to verify
- Inherited From:
- Overrides:
- Source:
-
write(request [, cb])
-
Send a message to the server. Sets the request headers passed into
headers
Calls listen to start listening for recieved data from server.
Ends connection when all data received from the server.
Emits a
serverDisconnected
event when connection is closed.Throws an error if there was an
error
event received when sending the requestParameters:
Name Type Argument Description request
string Stringified JSON-RPC message object
cb
function <optional>
Callback function to be called when message has been sent
- Overrides:
- Source: