Package org.web3j.protocol.websocket
Class WebSocketService
java.lang.Object
org.web3j.protocol.websocket.WebSocketService
- All Implemented Interfaces:
Web3jService
Web socket service that allows to interact with JSON-RPC via WebSocket protocol.
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 Summary
ConstructorDescriptionWebSocketService
(String serverUrl, boolean includeRawResponses) WebSocketService
(WebSocketClient webSocketClient, boolean includeRawResponses) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes resources used by the service.void
connect()
Connect to a WebSocket server.void
Returns the immutable versions of subscriptionForId map which represents the relation between subscription id and the associated subscription events.<T extends Response>
TPerform a synchronous JSON-RPC request.<T extends Response>
CompletableFuture<T>Performs an asynchronous JSON-RPC request.sendBatch
(BatchRequest requests) Perform a synchronous JSON-RPC batch requests.sendBatchAsync
(BatchRequest requests) Performs an asynchronous JSON-RPC batch requests.<T extends Notification<?>>
io.reactivex.Flowable<T>Subscribe to a stream of notifications.
-
Constructor Details
-
WebSocketService
-
WebSocketService
-
-
Method Details
-
connect
Connect to a WebSocket server.- Throws:
ConnectException
- thrown if failed to connect to the server via WebSocket protocol
-
connect
public void connect(Consumer<String> onMessage, Consumer<Throwable> onError, Runnable onClose) throws ConnectException - Throws:
ConnectException
-
getSubscriptionIdsMap
Returns the immutable versions of subscriptionForId map which represents the relation between subscription id and the associated subscription events. Is kept immutable because the only way in which the subscription should be updated is only through Dispose object.- Returns:
- immutable versions of subscriptionForId
-
send
Description copied from interface:Web3jService
Perform a synchronous JSON-RPC request.- Specified by:
send
in interfaceWeb3jService
- Type Parameters:
T
- type of a data item returned by the request- Parameters:
request
- request to performresponseType
- class of a data item returned by the request- Returns:
- deserialized JSON-RPC response
- Throws:
IOException
- thrown if failed to perform a request
-
sendAsync
Description copied from interface:Web3jService
Performs an asynchronous JSON-RPC request.- Specified by:
sendAsync
in interfaceWeb3jService
- Type Parameters:
T
- type of a data item returned by the request- Parameters:
request
- request to performresponseType
- class of a data item returned by the request- Returns:
- CompletableFuture that will be completed when a result is returned or if a request has failed
-
sendBatch
Description copied from interface:Web3jService
Perform a synchronous JSON-RPC batch requests.- Specified by:
sendBatch
in interfaceWeb3jService
- Parameters:
requests
- requests to perform- Returns:
- deserialized JSON-RPC responses
- Throws:
IOException
- thrown if failed to perform a batch request
-
sendBatchAsync
Description copied from interface:Web3jService
Performs an asynchronous JSON-RPC batch requests.- Specified by:
sendBatchAsync
in interfaceWeb3jService
- Parameters:
requests
- batch request to perform- Returns:
- CompletableFuture that will be completed when a result is returned or if a request has failed
-
subscribe
public <T extends Notification<?>> io.reactivex.Flowable<T> subscribe(Request request, String unsubscribeMethod, Class<T> responseType) Description copied from interface:Web3jService
Subscribe to a stream of notifications. A stream of notifications is opened by by performing a specified JSON-RPC request and is closed by calling the unsubscribe method. Different WebSocket implementations use different pair of subscribe/unsubscribe methods.This 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.
- Specified by:
subscribe
in interfaceWeb3jService
- Type Parameters:
T
- type of incoming event objects- Parameters:
request
- 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 stream- Returns:
- a
Flowable
instance that emits incoming events
-
close
public void close()Description copied from interface:Web3jService
Closes resources used by the service.- Specified by:
close
in interfaceWeb3jService
-