Skip to content

Instantly share code, notes, and snippets.

@pilate
Created May 17, 2011 19:54
Show Gist options
  • Save pilate/977250 to your computer and use it in GitHub Desktop.
Save pilate/977250 to your computer and use it in GitHub Desktop.
var http = require('http'),
net = require("net"),
fs = require("fs"),
Canvas = require('canvas');
var path_regex = /^\/apb\/status[\/]?$/;
var image_cache;
var servers = [{
"name": "Login",
"port": 1001,
"host": "apb.login.gamersfirst.com"
}, {
"name": "Colby",
"port": 1031,
"host": "173.195.41.34"
}, {
"name": "Joker",
"port": 1031,
"host": "173.195.39.20"
}, {
"name": "Patriot",
"port": 1031,
"host": "173.195.33.202"
}, {
"name": "Obeya",
"port": 1031,
"host": "173.195.41.33"
}];
var testServer = function (server) {
var stream = net.createConnection(server.port, server.host);
var tt = setTimeout(function () {
server.status = 0;
stream.destroy();
}, 2000);
stream.on("connect", function () {
server.status = 1;
stream.destroy();
});
stream.on("error", function () {
server.status = 0;
stream.destroy();
});
stream.on("close", function () {
// Update Cache
image_cache = getCanvas().toBuffer();
clearTimeout(tt);
});
};
var updateServers = function () {
for (var i = 0; i < servers.length; i++) {
var server = servers[i];
testServer(server);
}
};
var getCanvas = function () {
var offset = 10;
var canvas_obj = new Canvas(servers.length * 90, 30);
var context = canvas_obj.getContext('2d');
context.clearRect(0, 0, canvas_obj.width, canvas_obj.height);
context.font = '20px Impact Bold';
for (var i = 0; i < servers.length; i++) {
context.fillStyle = servers[i].status ? "green" : "red";
context.fillText(servers[i].name, offset, 20);
offset += 90;
}
return canvas_obj;
};
updateServers();
// Update status every 60 seconds
setInterval(updateServers, 60000);
http.createServer(function (request, response) {
if (path_regex.test(request.url)) {
response.writeHead(200, {
'Content-Type': 'image/png'
});
response.end(image_cache);
}
else {
response.writeHead(404, {
'Content-Type': 'text/html'
});
response.end("Not Found");
}
}).listen(64441);
console.log('APB Status Server Running');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment