2013-01-04 21:33:19 -05:00
|
|
|
var net = require('net')
|
|
|
|
, Client = require('./client')
|
2013-07-09 16:37:30 -04:00
|
|
|
, protocol = require('./protocol')
|
2013-01-04 21:33:19 -05:00
|
|
|
|
|
|
|
module.exports = ping;
|
|
|
|
|
|
|
|
function ping(options, cb) {
|
|
|
|
var host = options.host || 'localhost';
|
|
|
|
var port = options.port || 25565;
|
|
|
|
|
|
|
|
var client = new Client();
|
2013-01-27 19:36:22 -05:00
|
|
|
client.once(0xff, function(packet) {
|
2013-01-04 21:33:19 -05:00
|
|
|
var parts = packet.reason.split('\u0000');
|
|
|
|
var results;
|
|
|
|
try {
|
|
|
|
results = {
|
|
|
|
prefix: parts[0],
|
|
|
|
protocol: parseInt(parts[1], 10),
|
|
|
|
version: parts[2],
|
|
|
|
motd: parts[3],
|
|
|
|
playerCount: parseInt(parts[4], 10),
|
|
|
|
maxPlayers: parseInt(parts[5], 10),
|
2013-01-27 18:56:59 -05:00
|
|
|
latency: Date.now() - start
|
2013-01-04 21:33:19 -05:00
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
client.end();
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client.end();
|
|
|
|
cb(null, results);
|
|
|
|
});
|
|
|
|
client.on('error', function(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
client.on('connect', function() {
|
2013-07-07 05:04:15 -04:00
|
|
|
client.write(0xfe, {
|
2013-07-09 16:37:30 -04:00
|
|
|
readSuccessfully: 1,
|
2013-07-07 05:04:15 -04:00
|
|
|
customPayloadId: 250,
|
|
|
|
magicText: "MC|PingHost",
|
2013-07-07 14:25:59 -04:00
|
|
|
len: 3 + host.length + 4,
|
2013-07-09 16:37:30 -04:00
|
|
|
version: protocol.version,
|
2013-07-07 05:04:15 -04:00
|
|
|
ip: host,
|
|
|
|
port: port,
|
|
|
|
});
|
2013-01-04 21:33:19 -05:00
|
|
|
});
|
2013-01-27 18:56:59 -05:00
|
|
|
|
|
|
|
var start = Date.now();
|
2013-01-04 21:33:19 -05:00
|
|
|
client.connect(port, host);
|
|
|
|
}
|