Skip to content

Instantly share code, notes, and snippets.

@dynajoe
Created November 3, 2012 21:45
Show Gist options
  • Save dynajoe/4008957 to your computer and use it in GitHub Desktop.
Save dynajoe/4008957 to your computer and use it in GitHub Desktop.
Ping Server
var net = require('net');
var http = require('http');
var lastKey = "";
var sockets = [];
var netServer = net.createServer(function (socket) {
sockets.push(socket);
var removeClient = function () {
var index = sockets.indexOf(socket);
if (index != -1) sockets.splice(index, 1);
};
socket.on('end', removeClient);
socket.on('error', removeClient);
});
var httpServer = http.createServer(function (req, res) {
if (req.url.indexOf(lastKey) > 0)
{
console.log(req.connection.remoteAddress + ' won!');
console.log(req.url);
}
});
netServer.listen(8081);
httpServer.listen(8080);
setInterval(function () {
lastKey = new Date().getTime().toString();
for (var i = 0; i < sockets.length; i++) {
try {
var s = sockets[i];
s.write(lastKey);
} catch(e) { console.log(e);}
}
}, 2007);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment