-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
example.js
48 lines (36 loc) · 989 Bytes
/
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
'use strict';
const dgram = require('dgram');
const dtls = require('.');
const serverSocket = dgram.createSocket('udp4');
serverSocket.bind(4444);
const socket = dtls.connect({
type: 'udp4',
remotePort: 4444,
remoteAddress: '127.0.0.1',
maxHandshakeRetransmissions: 4,
});
socket.on('error', err => {
console.error('client:', err);
});
socket.on('data', data => {
console.log('client: got message "%s"', data.toString('ascii'));
socket.close();
});
socket.once('connect', () => {
socket.write('Hello from Node.js!');
});
socket.once('timeout', () => {
console.log('client: got timeout');
});
socket.setTimeout(5e3);
const server = dtls.createServer({ socket: serverSocket });
server.on('error', err => {
console.error('server:', err);
});
server.on('data', data => {
console.log('server: got message "%s"', data.toString('ascii'));
server.write(data, () => server.close());
});
socket.once('connect', () => {
console.log('server: got connecton');
});