Minetrack/lib/ping.js
Devin Ryan 43c284aa8a Show supported versions for PC servers
The Minetrack daemon will send a different protocol version each time
it pings a server. If a server responds with the same protocol version,
it is assumed that the version is supported, and it is shown on the
page above the server's player count.

The list of versions to be tried is stored in config.json.
At the moment, 4 versions are checked:
- 4 (Minecraft 1.7.2)
- 5 (Minecraft 1.7.10)
- 47 (Minecraft 1.8)
- 107 (Minecraft 1.9)
2016-03-01 21:09:38 -06:00

58 lines
1.6 KiB
JavaScript

var mcpe_ping = require('mcpe-ping');
var mcpc_ping = require('mc-ping-updated');
var util = require('./util');
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
function pingMinecraftPC(host, port, timeout, callback, version) {
var startTime = util.getCurrentTimeMs();
mcpc_ping(host, port, function(err, res) {
if (err) {
callback(err, null);
} else {
// Remap our JSON into our custom structure.
callback(null, {
players: {
online: res.players.online,
max: res.players.max
},
version: res.version.protocol,
latency: util.getCurrentTimeMs() - startTime,
favicon: res.favicon
});
}
}, timeout, version);
}
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
function pingMinecraftPE(host, port, timeout, callback) {
var startTime = util.getCurrentTimeMs();
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: parseInt(res.currentPlayers),
max: parseInt(res.maxPlayers)
},
version: res.version,
latency: util.getCurrentTimeMs() - startTime
});
}
}, timeout);
}
exports.ping = function(host, port, type, timeout, callback, version) {
if (type === 'PC') {
pingMinecraftPC(host, port || 25565, timeout, callback, version);
} else if (type === 'PE') {
pingMinecraftPE(host, port || 19132, timeout, callback);
} else {
throw new Error('Unsupported type: ' + type);
}
};