connectRelay method

Future<void> connectRelay({
  1. required String relay,
  2. HttpClient? customHttpClient,
  3. bool? shouldIgnoreConnectionException,
  4. void onConnectionSuccess(
    1. WebSocket 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,
  HttpClient? customHttpClient,
  bool? shouldIgnoreConnectionException,
  void Function(WebSocket webSocket)? onConnectionSuccess,
}) async {
  _client ??= _createCustomHttpClient();
  WebSocket? webSocket;

  try {
    webSocket = await WebSocket.connect(
      relay,
      compression: CompressionOptions.compressionOff,
      customClient: customHttpClient ?? _client!,
    );

    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;
    }
  }
}