From 36c087c1301adb26b5d9fda07cfa0a67feff283c Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Thu, 12 Dec 2024 13:32:09 -0500 Subject: [PATCH] Start mdns after setting listeners (#312) --- lib/src/rpc/dial.dart | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/src/rpc/dial.dart b/lib/src/rpc/dial.dart index 03c2a4372db..4c25f5b060a 100644 --- a/lib/src/rpc/dial.dart +++ b/lib/src/rpc/dial.dart @@ -175,26 +175,36 @@ Future _searchMdns(String address) async { const type = '_rpc._tcp'; final discovery = BonsoirDiscovery(type: type); await discovery.ready; - await discovery.start(); - // The duration of timeout was arbitrarily decided. - // 1 second seemed enough to allow the device to scan - // the local network for matches before moving on. - // The balance we are striking here is long enough to - // reliably scan the local network, but short enough to not - // noticeably lengthen the connection flow for the user. - - const timeout = Duration(seconds: 1); - await for (final event in discovery.eventStream!.timeout(timeout)) { + String? localAddress; + discovery.eventStream!.listen((event) { if (event.type == BonsoirDiscoveryEventType.discoveryServiceFound) { unawaited(event.service!.resolve(discovery.serviceResolver)); } else if (event.type == BonsoirDiscoveryEventType.discoveryServiceResolved) { final service = event.service! as ResolvedBonsoirService; if (service.name == targetName && service.host != null) { final host = service.host!.substring(0, service.host!.length - 1); - return ('$host:${service.port}'); + localAddress = '$host:${service.port}'; } } + }); + + await discovery.start(); + final startTime = DateTime.now(); + + // The duration of timeout was arbitrarily decided. + // 2 seconds seemed enough to allow the device to scan + // the local network for matches before moving on. + // The balance we are striking here is long enough to + // reliably scan the local network, but short enough to not + // noticeably lengthen the connection flow for the user. + const timeout = Duration(seconds: 2); + while (DateTime.now().difference(startTime) < timeout) { + await Future.delayed(const Duration(microseconds: 100)); + if (localAddress != null) { + await discovery.stop(); + return localAddress!; + } } await discovery.stop();