-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
76 lines (54 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
73
74
75
76
'use strict'
const { once } = require('events')
const fs = require('fs')
const path = require('path')
const { Client, Server } = require('./lib')
const fixtures = path.join(__dirname, 'test', 'fixtures')
const cert = fs.readFileSync(path.join(fixtures, 'cert.pem'))
const key = fs.readFileSync(path.join(fixtures, 'key.pem'))
const ca = cert
const host = 'localhost'
const port = 8888
const alice = new Client({ ca, host, port })
const bob = new Client({ ca, host, port })
const server = new Server({ cert, key })
const run = async () => {
await server.start(port, host)
console.log('Started server')
const pubKey = await alice.publishBundle()
console.log('Alice published bundle')
const plaintext = 'initial plaintext'
const sid = await bob.sendInitMessage(pubKey, plaintext)
console.log('Bob sent initial message')
await alice.recvInitMessage(sid)
console.log('Alice received initial message')
await Promise.all([
alice.connect(sid),
bob.connect(sid)
])
console.log('Alice and bob connected')
alice.on('message', msg => {
if (msg.sid === sid) {
console.log('Bob says: ' + msg.plaintext)
}
})
bob.on('message', msg => {
if (msg.sid === sid) {
console.log('Alice says: ' + msg.plaintext)
}
})
const promise = Promise.all([
once(alice, 'disconnect'),
once(bob, 'disconnect')
])
alice.send(sid, 'Hello "bob"')
bob.send(sid, 'Hello "alice"')
setTimeout(() => {
alice.disconnect(sid)
bob.disconnect(sid)
}, 1e3)
await promise
server.stop()
console.log('Stopped server')
}
run().catch(console.error)