forked from rellfy/discord-urpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiscordIPC.js
52 lines (46 loc) · 1.16 KB
/
DiscordIPC.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
const EventEmitter = require('events');
const { encode, decode, getIPC } = require('./util.js');
/*
Codes Key:
HANDSHAKE: 0,
FRAME: 1,
CLOSE: 2,
PING: 3,
PONG: 4,
*/
module.exports = class IPCTransport extends EventEmitter {
constructor(clientID) {
super();
this.clientID = clientID;
this.socket = null;
}
async connect() {
const socket = this.socket = await getIPC();
this.emit('open');
socket.write(encode(0, { v: 1, client_id: this.clientID }));
socket.pause();
socket.on('readable', () => {
decode(socket, ({op, data}) => {
switch (op) {
case 3: this.send(data, 4);
break;
case 1:
if (!data) return;
this.emit('message', data);
break;
case 2: this.emit('close', data);
break;
default: break;
}
});
});
socket.on('close', this.onClose.bind(this));
socket.on('error', this.onClose.bind(this));
}
onClose(e) { this.emit('close', e); }
send(data, op = 1) { this.socket.write(encode(op, data)); } //frame
close() {
this.send({}, 2); //close
this.socket.end();
}
};