-
Notifications
You must be signed in to change notification settings - Fork 1
/
ss.js
78 lines (69 loc) · 1.86 KB
/
ss.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
const net = require('net');
let shallow_clients = {};
let uid_taken={};
let shallow_terms = ["flare","pain","left","knee"];
let regex = new RegExp(shallow_terms.join('|'),'gi');
const server = net.createServer(socket=>{
socket.on('data',(data)=>{
let payload = data.toString('utf8');
let json = {};
try {
json = attempt(JSON.parse,payload);
} catch(e) {
socket.destroy();
}
switch (json.type) {
case "connect":
let uid = generate_uid();
socket.uid = uid;
shallow_clients[uid] = socket;
socket.write(JSON.stringify({type:'uid',uid:uid}));
break;
case "ping":
socket.write(JSON.stringify({type:"ping",ping:json.timestamp}));
break;
}
});
socket.on('error', (e) => {
delete shallow_clients[socket.uid];
socket.destroy();
console.error(e);
});
socket.on('end', function() {
socket.destroy()
});
})
server.listen(8008);
const attempt = (func, ...arguments) => {
try {
return func(...arguments);
} catch (e) {
return e;
}
}
function generate_uid(){
var taken = true;
let new_uid;
while (taken == true) {
new_uid = "xxxx-xxxx-xxxx-xxxx".replace(/x/g,function (c) {
return (Math.floor(Math.random()*16)).toString(16);
});
if (uid_taken[new_uid] == undefined) {
uid_taken[new_uid] = true;
taken = false;
}
}
return new_uid;
}
module.exports.buzz = (msg) => {
let amt = (msg.match(regex));//.length;
if (amt) {
amt = amt.length;
} else {
return;
}
let payload=JSON.stringify({type:"buzz",amt:amt});
for (const c in shallow_clients) {
shallow_clients[c].write(payload);
}
}