mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-30 03:05:37 -05:00
38194ee9b4
* passing test: responds to ping requests * `mc.ping` - ping a server for info * `createServer`: `options.timeout` -> `options.checkTimeoutInterval` * `Client` emits `connect` event
38 lines
856 B
JavaScript
38 lines
856 B
JavaScript
var net = require('net')
|
|
, Client = require('./client')
|
|
|
|
module.exports = ping;
|
|
|
|
function ping(options, cb) {
|
|
var host = options.host || 'localhost';
|
|
var port = options.port || 25565;
|
|
|
|
var client = new Client();
|
|
client.on(0xff, function(packet) {
|
|
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),
|
|
};
|
|
} catch (err) {
|
|
client.end();
|
|
cb(err);
|
|
return;
|
|
}
|
|
client.end();
|
|
cb(null, results);
|
|
});
|
|
client.on('error', function(err) {
|
|
cb(err);
|
|
});
|
|
client.on('connect', function() {
|
|
client.write(0xfe, { magic: 1 });
|
|
});
|
|
client.connect(port, host);
|
|
}
|