WebSocketExchange<T> constructor

WebSocketExchange<T>(
  1. JsonRpcRequest request
)

Tracks the request/response cycle of a JSON-RPC web socket method call.

The request is assigned its own unique id, which is used to match the request to its corresponding response.

The async response returns when a call is made to complete or completeError.

const RpcRequest request = RpcRequest(
  Method.getAccountInfo,
  params: ['3C4iYswhNe7Z2LJvxc9qQmF55rsKDUGdiuKVUGpTbRsK'],
  id: null // Must be null
);

final WebSocketExchange exchange = WebSocketExchange(request);
print(exchange.id); // 1

responseHandler(final Map<String, dynamic> json) { // Async handler
  final RpcResponse<int> response = RpcResponse.parse(json, cast<int>);
  exchange.complete(response);
}

final RpcResponse response = await exchange.response;

Implementation

WebSocketExchange(
  final JsonRpcRequest request,
):  request = request.id != null ? request : request.copyWith(id: ++_id), // Add a unique id.
    createdAt = DateTime.now().toUtc(),
    _completer = Completer.sync();