forked from shamblett/coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_observe_async.dart
83 lines (68 loc) · 2.16 KB
/
get_observe_async.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// ignore_for_file: avoid_print
/*
* Package : Coap
* Author : S. Hamblett <[email protected]>
* Date : 06/06/2018
* Copyright : S.Hamblett
*
* Multiple asynchronous requests, including observe
*/
import 'dart:async';
import 'package:coap/coap.dart';
import 'config/coap_config.dart';
FutureOr<void> main() async {
final conf = CoapConfig();
final baseUri = Uri(
scheme: 'coap',
host: 'californium.eclipseprojects.io',
port: conf.defaultPort,
);
final client = CoapClient(baseUri, config: conf);
// Create the request for the get request
final reqObs = CoapRequest.get(Uri(path: 'obs'));
try {
print('Observing /obs on ${baseUri.host}');
final obs = await client.observe(reqObs);
obs.listen((final e) {
print('/obs response: ${e.payloadString}');
});
final reqObsNon = CoapRequest(
Uri(path: 'obs-non'),
RequestMethod.get,
confirmable: false,
);
print('Observing /obs-non on ${baseUri.host}');
final obsNon = await client.observe(reqObsNon);
obsNon.listen((final e) {
print('/obs-non response: ${e.payloadString}');
});
final futures = <Future<void>>[];
print('Sending get /large to ${baseUri.host}');
futures.add(
client.get(Uri(path: 'large')).then(
(final resp) => print('/large response: ${resp.payloadString}'),
),
);
print('Sending get /test to ${baseUri.host}');
futures.add(
client
.get(Uri(path: 'test'))
.then((final resp) => print('/test response: ${resp.payloadString}')),
);
print('Sending get /separate to ${baseUri.host}');
futures.add(
client.get(Uri(path: 'separate')).then(
(final resp) => print('/separate response: ${resp.payloadString}'),
),
);
print('Waiting until get requests are done');
await Future.wait(futures);
await client.cancelObserveProactive(obs);
print('Waiting 20 seconds for /obs-non results');
await Future<void>.delayed(const Duration(seconds: 20));
await client.cancelObserveProactive(obsNon);
} on Exception catch (e) {
print('CoAP encountered an exception: $e');
}
client.close();
}