-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
239 lines (228 loc) · 7.08 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Created by julia on 24.01.2017.
*/
let EventEmitter = require('events');
let websocket = require('ws');
let OPCODE = require('./structures/types').MESSAGE_TYPES;
/**
* @class Worker - The client
* @param {string} shardToken - The secret token that is shared with nero
* @param {string} host - The host to connect to, has to be a valid connection url
*/
class Worker extends EventEmitter {
constructor(shardToken, host) {
super();
if (!shardToken) throw new Error('You need to pass in a secret to use!');
if (!host) throw new Error('You need to pass in a host to connect to!');
this.host = host;
this.token = shardToken;
this.connectionAttempts = 0;
this.ws = null;
this.shardId = null;
this.shardCount = null;
this.state = {ready: false, connected: false, hearbeat: -1};
this.hearbeatInterval = null;
this.hearbeatTimeout = null;
this.connect();
}
/**
* Connect to the host
*/
connect() {
this.ws = new websocket(this.host);
this.ws.on('open', () => {
this.connectionAttempts = 1;
this.onConnection();
});
this.ws.on('error', (err) => this.onError(err));
this.ws.on('close', (code, number) => this.onDisconnect(code, number));
}
/**
* Gets called when the client connected succesfully
*/
onConnection() {
this.state.connected = true;
this.ws.on('message', (msg) => {
this.onMessage(msg)
});
}
/**
* Gets called when an error occurs
* @param err - The error
*/
onError(err) {
this.emit('error', err);
this.reconnect();
}
/**
* Gets called when the websocket disconnected from the host
* @param code - the disconnect code
* @param number - /shrug
*/
onDisconnect(code, number) {
this.emit('err', `Disconnected with code ${code} and number ${number}`);
this.state.connected = false;
this.state.ready = false;
this.state.hearbeat = -1;
let time = this.generateInterval(this.connectionAttempts);
clearInterval(this.hearbeatInterval);
setTimeout(() => {
this.connectionAttempts++;
this.connect();
}, time);
}
/**
* Gets called when trying to manually reconnect
*/
reconnect() {
this.ws.close(1006, 'Trying to reconnect!');
}
/**
* Generates an exponential reconnect interval
* @param k - number of retries
* @return {number} - the new interval in milliseconds
*/
generateInterval(k) {
let maxInterval = (Math.pow(2, k) - 1) * 1000;
if (maxInterval > 30 * 1000) {
maxInterval = 30 * 1000;
}
return Math.random() * maxInterval;
}
/**
* When the websocket receives a message
* @param msg - the message
* @return {*} - dont mind that.
*/
onMessage(msg) {
//Try to parse the msg as json
try {
msg = JSON.parse(msg);
} catch (e) {
// console.error(msg);
this.emit('error', e);
return;
}
// console.log(msg);
//switch over the type of the message
switch (msg.op) {
/**
*
*/
case OPCODE.identify: {
// console.log(msg);
let message = {op: OPCODE.identify, shardToken: this.token};
if (this.shardCount && this.shardId) {
message.d = {sc: this.shardCount, sid: this.shardId};
}
this.ws.send(JSON.stringify(message));
return;
}
/**
* Gets called when the connection has been authenticated successfully, contains shard_id and shard_count
*/
case OPCODE.ready: {
// console.log(msg);
this.state.hearbeat = msg.d.heartbeat;
this.state.ready = true;
this.setupHeartbeat(msg.d.heartbeat);
this.shardId = msg.d.sid;
this.shardCount = msg.d.sc;
this.emit('ws_ready', (msg.d));
if (msg.d.reshard) {
this.emit('ws_reshard', (msg.d));
}
return;
}
/**
* Gets called when a message is not a system message but an actual message for the software using the client
*/
case OPCODE.message: {
this.emit(msg.d.event, msg.d.data);
return;
}
/**
* The heartbeat
*/
case OPCODE.hearbeat: {
clearTimeout(this.hearbeatTimeout);
// console.log(msg);
return;
}
/**
* Gets called when the token was not accepted by the server.
*/
case OPCODE.unauthorized: {
this.emit('error', `The token was not accepted by the server!`);
return;
}
/**
* Anything else
*/
default:
this.emit('error', `Unkown Message ${JSON.stringify(msg)}`);
return;
}
}
/**
* Setups the heartbeat
* @param beat - heartbeat to send to the server
*/
setupHeartbeat(beat) {
this.hearbeatInterval = setInterval(() => {
try {
this.ws.send(JSON.stringify({
op: OPCODE.hearbeat,
shardID: this.shardId,
shardToken: this.token
}));
this.hearbeatTimeout = setTimeout(() => {
this.emit('error', `The master did not respond to the heartbeat!`);
this.reconnect();
}, beat + 5000);
} catch (e) {
this.emit('error', e);
this.reconnect();
}
}, beat - 3000);
}
/**
* send a custom msg to the server
* @param event - id of the event
* @param msg - the msg
*/
send(event, msg) {
this.ws.send(JSON.stringify({
op: OPCODE.message,
shardToken: this.token,
shardID: this.shardId, d: {
event: event,
uwu: 'uwu',
origin: `worker-${process.pid}-${this.shardId}`,
data: msg,
sendedAt: Date.now(),
shardID: this.shardId
}
}));
}
/**
* send a custom msg to the server
* @param event - id of the event
* @param msg - the msg
*/
emitRemote(event, msg) {
this.ws.send(JSON.stringify({
op: OPCODE.message,
shardToken: this.token,
shardID: this.shardId, d: {
event: event,
origin: `worker-${process.pid}
-${this.shardId}`,
shardID: this.shardId,
data: msg,
sendedAt: Date.now()
}
}));
}
}
module.exports = Worker;