forked from shamblett/coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_resource_secure.dart
68 lines (57 loc) · 1.91 KB
/
get_resource_secure.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
// ignore_for_file: avoid_print
/*
* Package : Coap
* Author : S. Hamblett <[email protected]>,
* J. Romann <[email protected]
* Date : 05/02/2022
* Copyright : S.Hamblett
*
* Get requests with different accepted media types using CoAPS and a Pre-Shared
* Key.
*
* You need to have OpenSSL available on your system to be able to run this
* example. See the README file or the dtls2 documentation for more information.
*/
import 'dart:async';
import 'dart:convert';
import 'package:coap/coap.dart';
final identity = utf8.encode('Client_identity');
final preSharedKey = utf8.encode('secretPSK');
final pskCredentials =
PskCredentials(identity: identity, preSharedKey: preSharedKey);
PskCredentials pskCredentialsCallback(final String? identityHint) =>
pskCredentials;
class DtlsConfig extends DefaultCoapConfig {
@override
String? get dtlsCiphers => 'PSK-AES128-CCM8';
}
FutureOr<void> main() async {
final conf = DtlsConfig();
final baseUri = Uri(
scheme: 'coaps',
host: 'californium.eclipseprojects.io',
port: conf.defaultSecurePort,
);
final client = CoapClient(
baseUri,
config: conf,
pskCredentialsCallback: pskCredentialsCallback,
);
try {
print('Sending get /test to ${baseUri.host}');
var response = await client.get(Uri(path: 'test'));
print('/test response: ${response.payloadString}');
print('Sending get /multi-format (text) to ${baseUri.host}');
response = await client.get(Uri(path: 'multi-format'));
print('/multi-format (text) response: ${response.payloadString}');
print('Sending get /multi-format (xml) to ${baseUri.host}');
response = await client.get(
Uri(path: 'multi-format'),
accept: CoapMediaType.applicationXml,
);
print('/multi-format (xml) response: ${response.payloadString}');
} on Exception catch (e) {
print('CoAP encountered an exception: $e');
}
client.close();
}