PE support using the mcpe-ping module
This commit is contained in:
parent
0cbfcf60d7
commit
97aecdcc09
20
app.js
20
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();
|
||||
});
|
@ -14,6 +14,11 @@
|
||||
"name": "Overcast",
|
||||
"ip": "us.oc.tc",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Hypixel PE",
|
||||
"ip": "pe.hypixel.net",
|
||||
"type": "PE"
|
||||
}
|
||||
],
|
||||
"routes": {
|
||||
|
41
lib/ping.js
41
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);
|
||||
}
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user