public class WebSocketService extends java.lang.Object implements Web3jService
Allows to interact with JSON-RPC either by sending individual requests or by subscribing to a stream of notifications. To subscribe to a notification it first sends a special JSON-RPC request that returns a unique subscription id. A subscription id is used to identify events for a single notifications stream.
To unsubscribe from a stream of notifications it should send another JSON-RPC request.
| Constructor and Description |
|---|
WebSocketService(java.lang.String serverUrl,
boolean includeRawResponses) |
WebSocketService(WebSocketClient webSocketClient,
boolean includeRawResponses) |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes resources used by the service.
|
void |
connect()
Connect to a WebSocket server.
|
<T extends Response> |
send(Request request,
java.lang.Class<T> responseType)
Perform a synchronous JSON-RPC request.
|
<T extends Response> |
sendAsync(Request request,
java.lang.Class<T> responseType)
Performs an asynchronous JSON-RPC request.
|
<T extends Notification<?>> |
subscribe(Request request,
java.lang.String unsubscribeMethod,
java.lang.Class<T> responseType)
Subscribe to a stream of notifications.
|
public WebSocketService(java.lang.String serverUrl,
boolean includeRawResponses)
public WebSocketService(WebSocketClient webSocketClient, boolean includeRawResponses)
public void connect()
throws java.net.ConnectException
java.net.ConnectException - thrown if failed to connect to the server via WebSocket protocolpublic <T extends Response> T send(Request request, java.lang.Class<T> responseType) throws java.io.IOException
Web3jServicesend in interface Web3jServiceT - type of a data item returned by the requestrequest - request to performresponseType - class of a data item returned by the requestjava.io.IOException - thrown if failed to perform a requestpublic <T extends Response> java.util.concurrent.CompletableFuture<T> sendAsync(Request request, java.lang.Class<T> responseType)
Web3jServicesendAsync in interface Web3jServiceT - type of a data item returned by the requestrequest - request to performresponseType - class of a data item returned by the requestpublic <T extends Notification<?>> io.reactivex.Flowable<T> subscribe(Request request, java.lang.String unsubscribeMethod, java.lang.Class<T> responseType)
Web3jServiceThis method creates an Flowable that can be used to subscribe to new notifications. When a client unsubscribes from this Flowable the service unsubscribes from the underlying stream of events.
subscribe in interface Web3jServiceT - type of incoming event objectsrequest - JSON-RPC request that will be send to subscribe to a stream of eventsunsubscribeMethod - method that will be called to unsubscribe from a stream of
notificationsresponseType - class of incoming events objects in a streamFlowable instance that emits incoming eventspublic void close()
Web3jServiceclose in interface Web3jService