connectRelay method

Future<bool> connectRelay(
  1. String dirtyUrl, {
  2. int connectTimeout = DEFAULT_WEB_SOCKET_CONNECT_TIMEOUT,
})

Connect a new relay

Implementation

Future<bool> connectRelay(String dirtyUrl,
    {int connectTimeout = DEFAULT_WEB_SOCKET_CONNECT_TIMEOUT}) async {
  String? url = cleanRelayUrl(dirtyUrl);
  if (url == null) {
    return false;
  }
  if (blockedRelays.contains(url)) {
    return false;
  }
  try {
    if (relays[url] == null) {
      relays[url] =
          Relay(url: url, connectionSource: ConnectionSource.UNKNOWN);
    }
    relays[url]!.tryingToConnect();
    if (url.startsWith("wss://brb.io")) {
      relays[url]!.failedToConnect();
//        relays[url]!.stats.connectionErrors++;
      return false;
    }

    transports[url] = nostrTransportFactory(url);
    await transports[url]!.ready.timeout(Duration(seconds: connectTimeout),
        onTimeout: () {
      print("timed out connecting to relay $url");
    });

    startListeningToSocket(url);

    developer.log("connected to relay: $url");
    relays[url]!.succeededToConnect();
    //    relays[url]!.stats.connections++;
    getRelayInfo(url);
    return true;
  } catch (e) {
    print("!! could not connect to $url -> $e");
    transports.remove(url);
  }
  relays[url]!.failedToConnect();
  //relays[url]!.stats.connectionErrors++;
  return false;
}