Experiment with mdns to resolve a service

This commit is contained in:
Vishesh Handa
2020-09-24 20:44:56 +02:00
parent 289182b12e
commit 8032a5ca5a
3 changed files with 45 additions and 0 deletions

37
lib/main_dns.dart Normal file
View File

@ -0,0 +1,37 @@
import 'package:multicast_dns/multicast_dns.dart';
Future<void> main() async {
// Parse the command line arguments.
var name = '_dartobservatory._tcp.local';
name = '_gitjournal._tcp';
final MDnsClient client = MDnsClient();
// Start the client with default options.
await client.start();
print("Client started");
//var query = ResourceRecordQuery.serverPointer(name);
var query = ResourceRecordQuery.serverPointer(name);
// Get the PTR recod for the service.
await for (var ptr in client.lookup<PtrResourceRecord>(query)) {
print("Got ptr $ptr");
// Use the domainName from the PTR record to get the SRV record,
// which will have the port and local hostname.
// Note that duplicate messages may come through, especially if any
// other mDNS queries are running elsewhere on the machine.
await for (SrvResourceRecord srv in client.lookup<SrvResourceRecord>(
ResourceRecordQuery.service(ptr.domainName))) {
// Domain name will be something like
// - "io.flutter.example@some-iphone.local._dartobservatory._tcp.local"
final String bundleId =
ptr.domainName; //.substring(0, ptr.domainName.indexOf('@'));
print('Dart observatory instance found at '
'${srv.target}:${srv.port} for "$bundleId".');
}
}
client.stop();
print('Done.');
}

View File

@ -574,6 +574,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.6+3"
multicast_dns:
dependency: "direct main"
description:
name: multicast_dns
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.2"
mutex:
dependency: "direct main"
description:

View File

@ -56,6 +56,7 @@ dependencies:
in_app_purchase: ^0.3.4+5
flutter_plugin_android_lifecycle: ^1.0.8 # for fixing the build
timeago: ^2.0.27
multicast_dns: ^0.2.2
dev_dependencies:
flutter_launcher_icons: "^0.7.2"