-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
43 lines (35 loc) · 870 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
const { Server } = require('..')
// look for peers listed under this topic
const topic = Buffer.from(process.env.TOPIC, 'hex')
const keyPair = {
publicKey: Buffer.from(process.env.SERVER_PUBLIC_KEY, 'hex'),
secretKey: Buffer.from(process.env.SERVER_SECRET_KEY, 'hex')
}
const clientKeys = process.env.CLIENT_KEYS.split(',').map(k => Buffer.from(k.trim(), 'hex'))
var s = new Server({
topic,
keyPair,
clientKeys,
open (peer) {
console.log('Connected')
// setup peer state here
},
error (peer, err) {
console.error(err)
},
message (peer, message) {
peer.send(message.toString().toUpperCase())
},
close (peer) {
// Teardown here
console.log('Disconnected')
}
})
s.listen(function () {
console.log('Ready')
})
process.once('SIGTERM', shutdown)
process.once('SIGINT', shutdown)
function shutdown () {
s.close()
}