-
Notifications
You must be signed in to change notification settings - Fork 14
/
cancel_request.dart
48 lines (37 loc) · 1.28 KB
/
cancel_request.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
// ignore_for_file: avoid_print
/*
* Package : Coap
* Author : S. Hamblett <[email protected]>
* Date : 06/06/2018
* Copyright : S.Hamblett
*
* Cancelling retries of an ongoing request
*/
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: 'coap.me', port: conf.defaultPort);
final client = CoapClient(baseUri, config: conf);
final cancelThisReq = CoapRequest.get(Uri(path: 'doesNotExist'));
try {
// Ensure this request is not also cancelled
print('Sending async get /hello to ${baseUri.host}');
final helloRespFuture = client.get(Uri(path: 'hello'));
print('Sending async get /doesNotExist to ${baseUri.host}');
final ignoreThisFuture = client.send(cancelThisReq);
print('Cancelling get /doesNotExist retries');
client.cancel(cancelThisReq);
print('Ignoring /doesNotExist response future');
ignoreThisFuture.ignore();
final resp = await helloRespFuture;
print('/hello response: ${resp.payloadString}');
if (cancelThisReq.retransmits > 0) {
print('Expected 0 retransmits!');
}
} on Exception catch (e) {
print('CoAP encountered an exception: $e');
}
client.close();
}