connect static method

Future<JsonRpcClient> connect(
  1. Uri uri, {
  2. Duration timeout = const Duration(seconds: 3),
})

Opens a connection to the WebSocket at uri.

Throws a TimeoutException when the server doesn't complete the handshake within timeout, which then becomes the default timeout for every call.

Implementation

static Future<JsonRpcClient> connect(
  Uri uri, {
  Duration timeout = const Duration(seconds: 3),
}) async {
  final HttpClient http = HttpClient()..connectionTimeout = timeout;
  final Future<WebSocket> pending = WebSocket.connect(
    uri.toString(),
    customClient: http,
  );
  try {
    final WebSocket socket = await pending.timeout(timeout);
    return JsonRpcClient._(socket, timeout);
  } on TimeoutException {
    http.close(force: true);
    pending.then((socket) => socket.close(), onError: (Object _) {});
    throw TimeoutException('Timed out connecting to $uri', timeout);
  } finally {
    http.close();
  }
}