From 97aecdcc09b0af485bee2478ad1e435c19903aa0 Mon Sep 17 00:00:00 2001 From: Cryptkeeper Date: Mon, 2 Nov 2015 00:57:30 -0600 Subject: [PATCH] PE support using the mcpe-ping module --- app.js | 20 +++++++++++++------- config.json | 5 +++++ lib/ping.js | 41 ++++++++++++++++++++++++++++++++++++----- package.json | 1 + 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index 71af4e6..f85509a 100644 --- a/app.js +++ b/app.js @@ -7,20 +7,21 @@ var config = require('./config.json'); var networkHistory = []; var connectedClients = 0; -// Start our main loop that fires off pings. -setInterval(function() { +function pingAll() { var servers = config.servers; for (var i = 0; i < servers.length; i++) { // Make sure we lock our scope. (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. if (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. if (!networkHistory[network.ip]) { @@ -35,8 +36,8 @@ setInterval(function() { } _networkHistory.push({ - err: err, - result: result + error: err, + result: res }); // Make sure we never log too much. @@ -46,7 +47,7 @@ setInterval(function() { }); })(servers[i]); } -}, 2500); +} server.start(function() { // Track how many people are currently connected. @@ -74,4 +75,9 @@ server.start(function() { logger.log('info', 'Client disconnected, total clients: %d', connectedClients); }); }); + + // Start our main loop that fires off pings. + setInterval(pingAll, 2500); + + pingAll(); }); \ No newline at end of file diff --git a/config.json b/config.json index b278cc2..fc08d4d 100644 --- a/config.json +++ b/config.json @@ -14,6 +14,11 @@ "name": "Overcast", "ip": "us.oc.tc", "type": "PC" + }, + { + "name": "Hypixel PE", + "ip": "pe.hypixel.net", + "type": "PE" } ], "routes": { diff --git a/lib/ping.js b/lib/ping.js index b016132..8c35a53 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -1,5 +1,7 @@ -var mcpc = require('./mcpc_buffer'); var net = require('net'); +var mcpe_ping = require('mcpe-ping'); + +var mcpc = require('./mcpc_buffer'); function pingMinecraftPC(host, port, timeout, callback) { var client = new net.Socket(); @@ -57,10 +59,19 @@ function pingMinecraftPC(host, port, timeout, callback) { try { 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! - callback(null, json); + callback(null, res); } catch (err) { // Our data is corrupt? Fail hard. 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. function writePCBuffer(client, buffer) { var length = mcpc.createBuffer(); @@ -88,9 +119,9 @@ function writePCBuffer(client, buffer) { exports.ping = function(host, port, type, timeout, callback) { if (type === 'PC') { - pingMinecraftPC(host, port, timeout, callback); + pingMinecraftPC(host, port || 25565, timeout, callback); } else if (type === 'PE') { - + pingMinecraftPE(host, port || 19132, timeout, callback); } else { throw new Error('Unsupported type: ' + type); } diff --git a/package.json b/package.json index 56363bd..831ed9d 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A Minecraft server tracker that lets you focus on the basics.", "main": "app.js", "dependencies": { + "mcpe-ping": "0.0.3", "mime": "^1.3.4", "socket.io": "^1.3.7", "winston": "^2.0.0"