-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.js
51 lines (45 loc) · 1.4 KB
/
client.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
const ipc = require('node-ipc');
ipc.config.id = 'wick';
ipc.config.retry = 5000;
ipc.config.networkHost = 'localhost';
ipc.config.networkPort = 27020;
ipc.config.silent = true;
let BroadcastHooks = [];
let MessageReceives = [];
let MessageRequestID = 0;
ipc.connectToNet('wick', () => {
ipc.of.wick.on('app.broadcast', data => {
BroadcastHooks.filter(v => v.type == data.type).forEach(v => v.hook(data.data));
});
ipc.of.wick.on('app.receive_message', data => {
let request = MessageReceives.filter(v => v.request_id == data.request_id).pop();
if (!request) return;
request.resolve(data.data);
MessageReceives = MessageReceives.filter(v => v.request_id !== data.request_id);
});
console.log('Client Connected');
});
function AddBroadcastHook(type, hook) {
BroadcastHooks.push({
hook: hook,
type: type,
});
}
function SendMessage(type, data) {
return new Promise((resolve, reject) => {
MessageRequestID++;
let message_request = {
request_id: MessageRequestID,
resolve: resolve,
reject: reject,
};
ipc.of.wick.emit('app.send_message', {
request_id: MessageRequestID,
type: type,
data: data,
});
MessageReceives.push(message_request);
});
}
exports.AddBroadcastHook = AddBroadcastHook;
exports.SendMessage = SendMessage;