JsonRpcWebsocketClient<R> class

JSON RPC Websocket Client

Manages a single websocket connection to uri.

The protocols property specifies the subprotocols the client is willing to speak.

A connection fails after maxAttempts or timeLimit (whichever occurs first). The delay before each connection attempt can be configured by defining a backoffSchedule in milliseconds. If backoffSchedule length is less that maxAttempts, the final value in backoffSchedule is applied to all remaining attempts.

final socket = JsonRpcWebsocketClient(
  Uri.parse('wss://example_server'),
  maxAttempts: 3,                    // Attempt to establish a connect up to 3 times.
  backoffSchedule: [0, 1000],        // Trigger the first connection attempt immediately (0)
);                                   // & each remaining attempt after a 1 second delay (1000).

await socket.send('Hello');
Inheritance

Constructors

JsonRpcWebsocketClient.new(Uri uri, {Duration? timeLimit, int? maxAttempts, List<int>? backoffSchedule, List<String>? protocols, JsonRpcWebsocketClientOnConnect<R>? onConnect, JsonRpcWebsocketClientOnDisconnect? onDisconnect, JsonRpcWebsocketClientOnData<R>? onData, JsonRpcWebsocketClientOnError? onError, JsonRpcWebsocketClientOnPing<R>? onPing, required JsonRpcWebsocketClientIsPing<R> isPing, JsonRpcClientEncoder? encoder, required JsonRpcClientDecoder<R> decoder})
Creates a JSON RPC Client for Websocket methods.

Properties

backoffSchedule List<int>
The pre-connect backoff schedule.
final
decoder JsonRpcClientDecoder<R>
Converts the incoming responses of type R to JSON.
finalinherited
encoder JsonRpcClientEncoder
Converts the outgoing requests to byte data.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
isConnected bool
True if the socket is open.
no setter
isPing JsonRpcWebsocketClientIsPing<R>
Returns true if the received data is a PING message
final
maxAttempts int
The maximum number of connection attempts per connect() call.
final
onConnect JsonRpcWebsocketClientOnConnect<R>?
The callback triggered each time a connection is started.
final
onData JsonRpcWebsocketClientOnData<R>?
The websocket stream's data handler.
final
onDisconnect JsonRpcWebsocketClientOnDisconnect?
The callback triggered when the connection disconnects.
final
onError JsonRpcWebsocketClientOnError?
The websocket stream's error handler.
final
onPing JsonRpcWebsocketClientOnPing<R>?
The websocket stream's ping message handler.
final
protocols List<String>?
The subprotocols.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeLimit Duration?
The default timeout duration applied to each request.
finalinherited
uri Uri
The connecting endpoint.
finalinherited

Methods

connect({Duration? timeLimit}) Future<void>
Establishes a websocket connection to uri.
dispose() Future<void>
Closes the client and disposes of all acquired resources.
override
handler<T>(List<int> encoded, {JsonRpcClientConfig? config, int? id}) Future<T>
Sends an encoded JSON RPC request to uri and returns the decoded JSON response (Map or List of Maps).
override
handshake(List<int> data, {JsonRpcClientConfig? config}) Future<R>
Sends a raw data request to establish an encrypted session.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onTimeout<T>() FutureOr<T>
Creates a TimeoutException.
inherited
send<T>(JsonRpcRequest request, JsonRpcResponseDecoder<Map<String, dynamic>, JsonRpcResponse<T>> decode, {JsonRpcClientConfig? config}) Future<JsonRpcSuccessResponse<T>>
Sends a JSON RPC request to uri and returns its response.
inherited
sendAll<T>(List<JsonRpcRequest> request, JsonRpcResponseDecoder<List<Map<String, dynamic>>, List<JsonRpcResponse<T>>> decode, {JsonRpcClientConfig? config, bool eagerError = false}) Future<List<JsonRpcResponse<T>>>
Sends a batch JSON RPC request to uri and returns its response.
inherited
timeout<T>(Future<T> request, Duration? timeLimit) Future<T>
Creates a new Future that completes with the result of request or a TimeoutException if timeLimit elapses (default: Client.timeLimit).
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

withByteDecoder(Uri uri, {Duration? timeLimit, int? maxAttempts, List<int>? backoffSchedule, List<String>? protocols, JsonRpcWebsocketClientOnConnect<List<int>>? onConnect, JsonRpcWebsocketClientOnDisconnect? onDisconnect, JsonRpcWebsocketClientOnData<List<int>>? onData, JsonRpcWebsocketClientOnError? onError, JsonRpcWebsocketClientOnPing<List<int>>? onPing}) JsonRpcWebsocketClient<List<int>>
Creates a JSON RPC Client for Websocket methods that return byte data.
withStringDecoder(Uri uri, {Duration? timeLimit, int? maxAttempts, List<int>? backoffSchedule, List<String>? protocols, JsonRpcWebsocketClientOnConnect<String>? onConnect, JsonRpcWebsocketClientOnDisconnect? onDisconnect, JsonRpcWebsocketClientOnData<String>? onData, JsonRpcWebsocketClientOnError? onError, JsonRpcWebsocketClientOnPing? onPing}) JsonRpcWebsocketClient<String>
Creates a JSON RPC Client for Websocket methods that return String data.