-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (64 loc) · 2.37 KB
/
app.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
var WebSocket = require('ws');
var WebSocketServer = WebSocket.Server;
var http = require('http');
var sockets = [];
var server = new WebSocketServer({
port: 5000
});
var make_response = function(data, errors, notifications, status) {
data = typeof data !== 'undefined' ? data : {};
errors = typeof errors !== 'undefined' ? errors : [];
notifications = typeof notifications !== 'undefined' ? notifications : [];
status = typeof status !== 'undefined' ? status : 'OK';
return JSON.stringify({
data: data,
errors: errors,
notifications: notifications,
status: status
});
}
server.on('connection', function(socket) {
console.log('Client connected.');
sockets.push(socket);
console.log('Total clients: ' + sockets.length);
});
http.createServer(function (req, res) {
if (req.method == 'POST') {
var content = [];
req.on('data', function(data) {
content.push(data);
});
req.on('end', function() {
content = decodeURIComponent(content.join('')).split('&');
var new_content = {};
var temp;
for (i = 0; i < content.length; i++) {
temp = content[i].split('=');
new_content[temp[0]] = temp[1];
}
var alive_sockets = [];
var response = make_response(new_content);
for (var i = 0; i < sockets.length; i++) {
var socket = sockets[i];
if (socket.readyState == WebSocket.OPEN) {
socket.send(response);
alive_sockets.push(i);
} else
console.log('Client disconnected.');
}
var temp_sockets = [];
for (var i = 0; i < alive_sockets.length; i++)
temp_sockets.push(sockets[alive_sockets[i]]);
sockets = temp_sockets;
console.log('Total clients: ' + sockets.length);
console.log(req.connection.remoteAddress + '200 OK: ' + response);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(response);
});
} else {
console.log(req.socket.remoteAddress + '405 Unimplemented Method: "' + req.method + '"')
res.writeHead(405);
res.end();
}
}).listen(8000, '127.0.0.1');
console.log('Listening on 127.0.0.1:5000/8000...');