-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay-server.js
68 lines (59 loc) · 2.06 KB
/
relay-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
var net = require("net");
"use strict";
var clients = [];
var buffers = [];
exports.relayServer = function(serverPort) {
var server = net.createServer();
server.on("connection", function(socket) {
console.log(socket.remoteAddress + ":" + socket.remotePort);
clients.push(socket);
if(clients.length > 2) {
clients.splice(2,1);
console.log("There are " + clients.length + " connections already");
console.log("Dropping off the connection from: " + socket.remoteAddress + ":" + socket.remotePort);
socket.destroy();
}
if(clients.length === 1) {
console.log("Waiting for one more TCP connection.");
} else {
console.log("Condition met.");
if(buffers.length !== 0) {
for(var i=0;i<buffers.length;++i) {
clients[1].write(clients[0].remoteAddress + ":" + clients[0].remotePort + buffers[i]);
}
buffers = [];
}
}
socket.on("data", function(data) {
var idx = clients.indexOf(socket);
if(clients.length < 2) {
console.log("For data to be transmitted, two TCP clients must be connected.");
console.log("Currently buffering the data.");
buffers.push(data);
} else if(clients.length === 2 && idx !== -1) {
var clientToSend = idx ^ 1;
clients[clientToSend].write(socket.remoteAddress + ':' + socket.remotePort + ':' + data);
}
});
socket.on("close", function() {
console.log("Connection closed: " + socket.remoteAddress + ":" + socket.remotePort);
var idx = clients.indexOf(socket);
//console.log("Index while closing: " + idx);
if(idx !== -1)
clients.splice(idx,1);
if(clients.length === 0) {
console.log("No client is currently connected. Waiting for new connection.");
} else if (clients.length == 1) {
console.log("Waiting for one more client to be connected.");
}
});
socket.on("error", function(err) {
console.log("Error occured from connection: " + socket.remoteAddress + ":" + socket.remotePort);
console.log("Error: %s", err);
});
});
server.listen(serverPort, function() {
console.log("The server is listening on %j", server.address());
});
return server;
}