Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions mcping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

const net = require('net');
const dgram = require('dgram');
const process = require('process');

const STARTED = 0;
const SENT_CHALLENGE_REQUEST = 1;
const SENT_STATUS_REQUEST = 2;
const DONE = 3;

// FE01FA ping compatible with 1.4.4, 1.5.2, 1.6.4(*), 1.7.10, 1.8.9, 1.9
// (*) MC|PingHost is required for 1.6.4, or it'll take ~2 seconds to get a reply
Expand Down Expand Up @@ -33,14 +37,11 @@ function ping_fe(options, cb) {
function ping_fefd_udp(options, cb) {
const host = options.host;
const port = options.port;
const timeout = options.timeout || 2000;
const udp = dgram.createSocket('udp4');

//console.log('udp ping');

const STARTED = 0;
const SENT_CHALLENGE_REQUEST = 1;
const SENT_STATUS_REQUEST = 2;
const DONE = 3;
let state = 0;

udp.on('error', (err) => {
Expand Down Expand Up @@ -104,7 +105,11 @@ function ping_fefd_udp(options, cb) {
result.maxPlayers = parseInt(array[22]);
result.port = parseInt(array[24]);
result.host = array[26];
// TODO: online players comes last, parse it
var players = [];
for(var i=30;i<array.length-2;i++){
players.push(array[i]);
}
result.players = players;
//console.log('result',result);
state = DONE;
cb(null, result, 'fefd_udp');
Expand All @@ -123,6 +128,13 @@ function ping_fefd_udp(options, cb) {
});

udp.bind();

setTimeout(function(){
if (state !== DONE){
cb('timeout', null, 'fefd_udp');
}
udp.close();
}, timeout);
}

function _ping_buffer(options, cb, buffer, type) {
Expand Down