2015-11-02 00:57:30 -06:00
|
|
|
var mcpe_ping = require('mcpe-ping');
|
2015-11-02 01:42:11 -06:00
|
|
|
var mcpc_ping = require('mc-ping-updated');
|
2015-11-01 22:56:08 -06:00
|
|
|
|
2015-11-08 18:34:17 -06:00
|
|
|
var util = require('./util');
|
2015-11-03 01:40:09 -06:00
|
|
|
|
2015-11-02 02:24:55 -06:00
|
|
|
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
|
2016-03-01 21:09:38 -06:00
|
|
|
function pingMinecraftPC(host, port, timeout, callback, version) {
|
2015-11-08 18:34:17 -06:00
|
|
|
var startTime = util.getCurrentTimeMs();
|
2015-11-01 22:56:08 -06:00
|
|
|
|
2015-11-25 19:29:00 -06:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
2016-03-01 21:09:38 -06:00
|
|
|
}, timeout, version);
|
2015-11-02 01:04:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
|
2015-11-02 00:57:30 -06:00
|
|
|
function pingMinecraftPE(host, port, timeout, callback) {
|
2015-11-08 18:34:17 -06:00
|
|
|
var startTime = util.getCurrentTimeMs();
|
2015-11-25 19:29:00 -06:00
|
|
|
|
|
|
|
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);
|
2015-11-02 00:57:30 -06:00
|
|
|
}
|
|
|
|
|
2016-03-01 21:09:38 -06:00
|
|
|
exports.ping = function(host, port, type, timeout, callback, version) {
|
2015-11-01 22:56:08 -06:00
|
|
|
if (type === 'PC') {
|
2016-06-09 13:26:32 +10:00
|
|
|
util.unfurlSRV(host, port, function(host, port){
|
|
|
|
console.log('Pinging '+host+":"+port+"...")
|
|
|
|
pingMinecraftPC(host, port || 25565, timeout, callback, version);
|
|
|
|
})
|
2015-11-01 22:56:08 -06:00
|
|
|
} else if (type === 'PE') {
|
2015-11-02 00:57:30 -06:00
|
|
|
pingMinecraftPE(host, port || 19132, timeout, callback);
|
2015-11-01 22:56:08 -06:00
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported type: ' + type);
|
|
|
|
}
|
2016-03-01 21:09:38 -06:00
|
|
|
};
|