PE support using the mcpe-ping module

This commit is contained in:
Cryptkeeper 2015-11-02 00:57:30 -06:00
parent 0cbfcf60d7
commit 97aecdcc09
4 changed files with 55 additions and 12 deletions

20
app.js

@ -7,20 +7,21 @@ var config = require('./config.json');
var networkHistory = []; var networkHistory = [];
var connectedClients = 0; var connectedClients = 0;
// Start our main loop that fires off pings. function pingAll() {
setInterval(function() {
var servers = config.servers; var servers = config.servers;
for (var i = 0; i < servers.length; i++) { for (var i = 0; i < servers.length; i++) {
// Make sure we lock our scope. // Make sure we lock our scope.
(function(network) { (function(network) {
ping.ping(network.ip, network.port || 25565, network.type, 2500, function(err, result) { ping.ping(network.ip, network.port, network.type, 2500, function(err, res) {
// Handle our ping results, if it succeeded. // Handle our ping results, if it succeeded.
if (err) { if (err) {
logger.log('error', 'Failed to ping ' + network.ip + ': ' + err); logger.log('error', 'Failed to ping ' + network.ip + ': ' + err);
} else {
console.log(network.ip + ': ' + res.players.online);
} }
server.io.sockets.emit('update', result); server.io.sockets.emit('update', res);
// Log our response. // Log our response.
if (!networkHistory[network.ip]) { if (!networkHistory[network.ip]) {
@ -35,8 +36,8 @@ setInterval(function() {
} }
_networkHistory.push({ _networkHistory.push({
err: err, error: err,
result: result result: res
}); });
// Make sure we never log too much. // Make sure we never log too much.
@ -46,7 +47,7 @@ setInterval(function() {
}); });
})(servers[i]); })(servers[i]);
} }
}, 2500); }
server.start(function() { server.start(function() {
// Track how many people are currently connected. // Track how many people are currently connected.
@ -74,4 +75,9 @@ server.start(function() {
logger.log('info', 'Client disconnected, total clients: %d', connectedClients); logger.log('info', 'Client disconnected, total clients: %d', connectedClients);
}); });
}); });
// Start our main loop that fires off pings.
setInterval(pingAll, 2500);
pingAll();
}); });

@ -14,6 +14,11 @@
"name": "Overcast", "name": "Overcast",
"ip": "us.oc.tc", "ip": "us.oc.tc",
"type": "PC" "type": "PC"
},
{
"name": "Hypixel PE",
"ip": "pe.hypixel.net",
"type": "PE"
} }
], ],
"routes": { "routes": {

@ -1,5 +1,7 @@
var mcpc = require('./mcpc_buffer');
var net = require('net'); var net = require('net');
var mcpe_ping = require('mcpe-ping');
var mcpc = require('./mcpc_buffer');
function pingMinecraftPC(host, port, timeout, callback) { function pingMinecraftPC(host, port, timeout, callback) {
var client = new net.Socket(); var client = new net.Socket();
@ -57,10 +59,19 @@ function pingMinecraftPC(host, port, timeout, callback) {
try { try {
var json = JSON.parse(buffer.readString()); var json = JSON.parse(buffer.readString());
json.latency = (new Date).getTime() - milliseconds; // Remap our JSON into our custom structure.
var res = {
players: json.players,
version: json.version.protocol,
latency: (new Date).getTime() - milliseconds
};
if (json.favicon) {
res.favicon = json.favicon;
}
// We parsed it, send it along! // We parsed it, send it along!
callback(null, json); callback(null, res);
} catch (err) { } catch (err) {
// Our data is corrupt? Fail hard. // Our data is corrupt? Fail hard.
callback(err, null); callback(err, null);
@ -77,6 +88,26 @@ function pingMinecraftPC(host, port, timeout, callback) {
}); });
} }
function pingMinecraftPE(host, port, timeout, callback) {
var milliseconds = (new Date).getTime();
mcpe_ping(host, port || 19132, function(err, res) {
if (err) {
callback(err, null);
} else {
// Remap our JSON into our custom structure.
callback(err, {
players: {
online: res.currentPlayers,
max: res.maxPlayers
},
version: res.version,
latency: (new Date).getTime() - milliseconds
});
}
}, timeout);
}
// Wraps our Buffer into another to fit the Minecraft protocol. // Wraps our Buffer into another to fit the Minecraft protocol.
function writePCBuffer(client, buffer) { function writePCBuffer(client, buffer) {
var length = mcpc.createBuffer(); var length = mcpc.createBuffer();
@ -88,9 +119,9 @@ function writePCBuffer(client, buffer) {
exports.ping = function(host, port, type, timeout, callback) { exports.ping = function(host, port, type, timeout, callback) {
if (type === 'PC') { if (type === 'PC') {
pingMinecraftPC(host, port, timeout, callback); pingMinecraftPC(host, port || 25565, timeout, callback);
} else if (type === 'PE') { } else if (type === 'PE') {
pingMinecraftPE(host, port || 19132, timeout, callback);
} else { } else {
throw new Error('Unsupported type: ' + type); throw new Error('Unsupported type: ' + type);
} }

@ -4,6 +4,7 @@
"description": "A Minecraft server tracker that lets you focus on the basics.", "description": "A Minecraft server tracker that lets you focus on the basics.",
"main": "app.js", "main": "app.js",
"dependencies": { "dependencies": {
"mcpe-ping": "0.0.3",
"mime": "^1.3.4", "mime": "^1.3.4",
"socket.io": "^1.3.7", "socket.io": "^1.3.7",
"winston": "^2.0.0" "winston": "^2.0.0"