-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
73 lines (56 loc) · 1.58 KB
/
example.js
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
'use strict';
/* eslint-disable require-jsdoc */
const dgram = require('dgram');
const Emitter = require('events');
const { dtls, constants } = require('.');
const { GNUTLS_CLIENT, GNUTLS_DATAGRAM } = constants;
dtls.set_debug_mode(true);
const socket = dgram.createSocket('udp4');
const sync = new Emitter();
const queue = [];
let ready = true;
socket.connect(4444, 'localhost', () => {
console.log('connected to dtls server');
const session = dtls.create_session(GNUTLS_CLIENT | GNUTLS_DATAGRAM);
dtls.set_priority(session, 'NORMAL:+AEAD:+ECDHE-ECDSA:+ECDHE-RSA:+RSA:+PSK:+ECDHE-PSK:+VERS-DTLS1.2');
dtls.set_mtu(session, 1200);
dtls.send(session, (data) => {
console.log('send %s bytes', data.length);
socket.send(data, () => console.log('send successful'));
});
socket.on('message', buf => {
queue.push(buf);
if (ready) {
setImmediate(() => sync.emit('ready'));
}
});
dtls.handshake(session, (error) => {
if (error) {
console.log('handshake error', error);
} else {
console.log('handshake successful')
}
socket.disconnect();
socket.close();
});
sync.on('ready', () => {
if (!ready) {
return;
}
ready = false;
const buf = queue.shift();
console.log('recv %s bytes', buf.length);
dtls.recv(session, buf, (error) => {
ready = true;
if (error) {
console.error('fail to recv data');
socket.close();
return;
}
console.error('recv successful');
if (queue.length) {
setImmediate(() => sync.emit('ready'));
}
});
});
});