connectRelay method

Future<void> connectRelay({
  1. required String relay,
  2. bool? shouldIgnoreConnectionException,
  3. void onConnectionSuccess(
    1. WebSocketChannel webSocket
    )?,
})

Connects to a relay web socket, and trigger the onConnectionSuccess callback if the connection is successful, or the onConnectionError callback if the connection fails.

Implementation

Future<void> connectRelay({
  required String relay,
  bool? shouldIgnoreConnectionException,
  void Function(WebSocketChannel webSocket)? onConnectionSuccess,
}) async {
  WebSocketChannel? webSocket;

  try {
    webSocket = WebSocketChannel.connect(
      Uri.parse(relay),
    );

    await webSocket.ready;

    onConnectionSuccess?.call(webSocket);
  } catch (e) {
    utils.log(
      'error while connecting to the relay with url: $relay',
      e,
    );

    if (shouldIgnoreConnectionException ?? true) {
      utils.log(
        'The error related to relay: $relay is ignored, because to the ignoreConnectionException parameter is set to true.',
      );
    } else {
      rethrow;
    }
  }
}