-
Notifications
You must be signed in to change notification settings - Fork 70
/
server-ipc.js
50 lines (44 loc) · 1.18 KB
/
server-ipc.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
const ipc = require('node-ipc')
function init(socketName, handlers) {
ipc.config.id = socketName
ipc.config.silent = true
ipc.serve(() => {
ipc.server.on('message', (data, socket) => {
let msg = JSON.parse(data)
let { id, name, args } = msg
if (handlers[name]) {
handlers[name](args).then(
result => {
ipc.server.emit(
socket,
'message',
JSON.stringify({ type: 'reply', id, result })
)
},
error => {
// Up to you how to handle errors, if you want to forward
// them, etc
ipc.server.emit(
socket,
'message',
JSON.stringify({ type: 'error', id })
)
throw error
}
)
} else {
console.warn('Unknown method: ' + name)
ipc.server.emit(
socket,
'message',
JSON.stringify({ type: 'reply', id, result: null })
)
}
})
})
ipc.server.start()
}
function send(name, args) {
ipc.server.broadcast('message', JSON.stringify({ type: 'push', name, args }))
}
module.exports = { init, send }