-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
136 lines (116 loc) · 3.18 KB
/
server.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
const WebSocketServer = require('ws').Server;
const Client = require('./client.js');
var request = require('request');
var port = process.env.PORT || 8080
var http = require("http")
var express = require("express")
var app = express()
app.use(express.static(__dirname + "/"))
var server = http.createServer(app)
server.listen(port)
let clients = [];
function clientFromWebSocket(ws) {
return clients.find(function(client) {
return client.websocket === ws;
});
};
function clientFromID(id) {
return clients.find(function(client) {
return client.id == id;
});
};
function removeSocket(ws) {
var socket = clientFromWebSocket(ws);
for(var i = clients.length - 1; i >= 0; i--) {
if(clients[i] === socket) {
clients.splice(i, 1);
}
}
}
function sender(form) {
return form
}
function extract(responseData) {
//TODO: fail with sensible error message
var parsedOnce = JSON.parse(responseData);
return parsedOnce
}
// Heartbeat type functions:
function noop() {}
function heartbeat() {
this.isAlive = true;
}
const wss = new WebSocketServer({server: server})
console.log("Websocket server listening on port: ", port);
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
wss.on('connection', function connection(ws) {
var newClient = new Client(guid(), ws);
clients.push(newClient);
ws.isAlive = true;
ws.on('pong', heartbeat);
ws.on('message', function incoming(message) {
var client = clientFromWebSocket(ws);
if (client == null) {
console.log("client not found!"); // should not happen.
return;
}
let payload
try {
payload = JSON.parse(message)
} catch(error) {
console.log("error: ", error)
ws.send(error.toString());
return
}
if (!('endpoint' in payload)) {
ws.send("Include 'endpoint' in JSON");
return
}
let endpoint = payload['endpoint'];
var toPost = JSON.stringify({sender: client.id, payload: payload})
request.post({url:endpoint,form: toPost}, function (error, response, responseData) {
if (error != null) {
ws.send(JSON.stringify(error));
return;
}
var extracted = extract(responseData);
var client = clientFromID(extracted.receiver);
if (client == null) {
console.log("Endpoint did not provide receiver client!!");
}
if (client) {
var responsePayload = JSON.stringify(extracted.payload);
client.websocket.send(responsePayload);
}
});
});
ws.on('close', function() {
removeSocket(ws)
console.log("client removed");
console.log("clients.length:", clients.length);
});
ws.on('error', function(e) {
if (e.code === "ECONNRESET") {
return;
}
console.log('Websocket ERROR:', e);
});
});
//TODO: timer to ping all the websockets periodically
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
removeSocket(ws)
return ws.terminate();
}
ws.isAlive = false;
ws.ping(noop);
});
}, 30000);