-
Notifications
You must be signed in to change notification settings - Fork 34
/
ipc.js
81 lines (67 loc) · 1.51 KB
/
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
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
81
class Ipc {
static get config() {
return {
id: 'screenCastWindow',
retry: 1000,
socketRoot: 'tmp',
networkHost: 'localhost',
appSpace:'MMM-Screencast',
port: 8123,
silent: true
};
}
static get socketId() {
return 'screenCastWindow';
}
static get socketDomain() {
return `/${Ipc.config.socketRoot}/${Ipc.config.appSpace}.${Ipc.socketId}`;
}
constructor() {
this.instance = require('node-ipc');
this.instance.config = {
...this.instance.config,
...Ipc.config
};
}
}
class IpcServer extends Ipc {
constructor() {
super();
this.instance.serve(Ipc.socketDomain);
this.instance.server.start();
}
get server() {
return this.instance.server;
}
on(type, cb = (data, socket) => null) {
return this.server.on(type, (data, socket) => cb(data, socket));
}
emit(socket, type, payload) {
return this.server.emit(socket, type, payload);
}
broadcast(type, payload) {
this.server.broadcast(type, payload);
}
}
class IpcClient extends Ipc {
constructor(cb = (self) => null) {
super();
this.instance.connectTo(Ipc.socketId, Ipc.socketDomain, () => cb(this));
}
get client() {
return this.instance.of[Ipc.socketId];
}
emit(type, payload) {
this.client.emit(type, payload);
}
on(type, cb = (data) => null) {
this.client.on(type, (data) => cb(data));
}
disconnect() {
this.instance.disconnect(Ipc.socketId);
}
}
module.exports = {
IpcServer,
IpcClient
};