Minetrack/lib/ping.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-11-02 06:57:30 +00:00
var mcpe_ping = require('mcpe-ping');
2015-11-02 07:42:11 +00:00
var mcpc_ping = require('mc-ping-updated');
var profiler = require('./profiler');
// 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) {
profiler.track(host);
// Try catch incase the down stream module is bad at handling exceptions.
try {
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: profiler.untrack(host)
});
}
}, timeout);
} catch (err) {
callback(err, null);
}
2015-11-02 07:04:49 +00:00
}
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
2015-11-02 06:57:30 +00:00
function pingMinecraftPE(host, port, timeout, callback) {
profiler.track(host);
2015-11-02 06:57:30 +00:00
// Try catch incase the down stream module is bad at handling exceptions.
try {
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: profiler.untrack(host)
});
}
}, timeout);
} catch (err) {
callback(err, null);
}
2015-11-02 06:57:30 +00:00
}
exports.ping = function(host, port, type, timeout, callback) {
if (type === 'PC') {
2015-11-02 06:57:30 +00:00
pingMinecraftPC(host, port || 25565, timeout, callback);
} else if (type === 'PE') {
2015-11-02 06:57:30 +00:00
pingMinecraftPE(host, port || 19132, timeout, callback);
} else {
throw new Error('Unsupported type: ' + type);
}
};