2016-07-05 14:36:29 -05:00
|
|
|
var mcpe_ping = require('mcpe-ping-fixed');
|
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 {
|
2019-09-05 23:15:44 +02:00
|
|
|
// Remap our JSON into our custom structure.
|
|
|
|
var favicon;
|
|
|
|
|
|
|
|
// Ensure the returned favicon is a data URI
|
|
|
|
if (res.favicon.indexOf('data:image/') === 0) {
|
|
|
|
favicon = res.favicon;
|
|
|
|
}
|
|
|
|
|
2015-11-25 19:29:00 -06:00
|
|
|
callback(null, {
|
|
|
|
players: {
|
2019-09-05 23:15:44 +02:00
|
|
|
online: parseInt(res.players.online),
|
|
|
|
max: parseInt(res.players.max)
|
2015-11-25 19:29:00 -06:00
|
|
|
},
|
2019-09-05 23:15:44 +02:00
|
|
|
version: parseInt(res.version.protocol),
|
2015-11-25 19:29:00 -06:00
|
|
|
latency: util.getCurrentTimeMs() - startTime,
|
2019-09-05 23:15:44 +02:00
|
|
|
favicon
|
2015-11-25 19:29:00 -06:00
|
|
|
});
|
|
|
|
}
|
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){
|
|
|
|
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
|
|
|
};
|