connectRelay method

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

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) {
    NostrClientUtils.log(
      "error while connecting to the relay with url: $relay",
      e,
    );

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