Skip to content

Commit

Permalink
feat: add DNS-SD CoAP example
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Feb 21, 2023
1 parent 6b19a16 commit e548bb7
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions example/coap_dns_sd_discovery.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 The NAMIB Project Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

// ignore_for_file: avoid_print

import 'package:dart_wot/dart_wot.dart';

const propertyName = 'string';

extension PrintExtension on InteractionOutput {
Future<void> printValue() async {
print(await value());
}
}

void handleThingDescription(ThingDescription thingDescription) =>
print('Discovered TD with title "${thingDescription.title}".');

Future<void> main(List<String> args) async {
final servient = Servient()..addClientFactory(CoapClientFactory());

final wot = await servient.start();
final uri = Uri.parse('_wot._udp.local');

// Example using for-await-loop
try {
await for (final thingDescription
in wot.discover(uri, method: DiscoveryMethod.dnsServiceDiscovery)) {
handleThingDescription(thingDescription);
}
print('Discovery with "await for" has finished.');
} on Exception catch (error) {
print(error);
}

// Example using the .listen() method, allowing for error handling
//
// Notice how the "onDone" callback is called before the result is passed
// to the handleThingDescription function.
wot.discover(uri, method: DiscoveryMethod.dnsServiceDiscovery).listen(
handleThingDescription,
onError: (error) => print('Encountered an error: $error'),
onDone: () => print('Discovery with "listen" has finished.'),
);
}

0 comments on commit e548bb7

Please sign in to comment.