-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver_ws.js
40 lines (30 loc) · 929 Bytes
/
server_ws.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
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 3200 });
var messages = [];
wss.on('connection', function connection(ws) {
var timeout;
try {
ws.send(JSON.stringify({
id: Math.floor(Date.now() / 1000),
type: 'CAT',
payload: 'http://thecatapi.com/api/images/get.php?pub_id=' + makeid()
}));
} catch (e) { }
ws.on('close', function close() {
clearTimeout(timeout);
});
timeout = setInterval(function() {
ws.send(JSON.stringify({
id: Math.floor(Date.now() / 1000),
type: 'CAT',
payload: 'http://thecatapi.com/api/images/get.php?pub_id=' + makeid()
}));
}, 5000);
});
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}