-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (66 loc) · 1.59 KB
/
index.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
77
78
79
80
const { info, warn } = require('ara-console')
const signalhub = require('ara-network/signalhub')
const extend = require('extend')
const debug = require('debug')('ara:network:node:signalhub')
const conf = {
maxBroadcasts: Infinity,
port: 8881,
}
let server = null
async function getInstance() {
return server
}
async function configure(opts, program) {
if (program) {
const { argv } = program
.option('port', {
alias: 'p',
describe: 'Port for server to listen on'
})
if (argv.port) { opts.port = argv.port }
}
return extend(true, conf, opts)
}
async function start() {
if (server) { return false }
server = signalhub.createServer(conf)
server.listen(conf.port)
server.on('error', onerror)
server.on('close', onclose)
server.on('publish', onpublish)
server.on('listening', onlistening)
server.on('subscribe', onsubscribe)
return true
function onerror(err) {
warn('signalhub: error:', err.message)
debug('error:', err)
}
function onclose() {
warn('signalhub: Closed')
}
function onpublish(channel) {
info("signalhub: Publishing channel '%s'", channel)
}
function onlistening() {
const { port } = server.address()
info('signalhub: Listening on port %s', port)
}
function onsubscribe(channel) {
info("signalhub: Subscribeing to channel '%s'", channel)
}
}
async function stop() {
if (null == server) { return false }
warn('signalhub: Stopping server')
server.close(onclose)
return true
function onclose() {
server = null
}
}
module.exports = {
getInstance,
configure,
start,
stop,
}