connectRelay method

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

Connect a new relay

Implementation

Future<bool> connectRelay(String url, {int connectTimeout=3}) async {
  relays[url] = Relay(url);
  relays[url]!.connecting = true;
  webSockets[url] = await WebSocket.connect(url).timeout(Duration(seconds: connectTimeout)).onError((error, stackTrace) {
    relays[url]!.connecting = false;
    print("could not connect to relay $url error:$error");
    throw Exception();
  });

  relays[url]!.connecting = false;

  webSockets[url]!.listen((message) {
    _handleIncommingMessage(message, url);
  }, onError: (error) async {
    /// todo: handle this better
    throw Exception("Error in socket");
  }, onDone: () {
    /// todo: handle this better
  });

  if (isWebSocketOpen(url)) {
    developer.log("connected to relay: $url");
    return true;
  }
  return false;
}