From dfcb960d40a5866b258999f787ba88cd76689ea8 Mon Sep 17 00:00:00 2001 From: phit Date: Wed, 15 Mar 2017 02:09:46 +0100 Subject: [PATCH 1/3] add players array to fefd_udp --- mcping.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mcping.js b/mcping.js index f4d7abe..9c6ae63 100644 --- a/mcping.js +++ b/mcping.js @@ -104,7 +104,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 Date: Thu, 3 Aug 2017 20:59:16 +0200 Subject: [PATCH 2/3] add timeout if server takes too long to respond --- mcping.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mcping.js b/mcping.js index 9c6ae63..d7cfa39 100644 --- a/mcping.js +++ b/mcping.js @@ -127,6 +127,15 @@ function ping_fefd_udp(options, cb) { }); udp.bind(); + + function timeout() { + setTimeout(function(){ + if(state !== 3){ + udp.close;cb('timeout', null, 'fefd_udp'); + } + }, 2000); + } + timeout(); } function _ping_buffer(options, cb, buffer, type) { From cd060f44851ce8bd4ae0c822e41db4bdabc5f7da Mon Sep 17 00:00:00 2001 From: phit <2097483+phit@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:47:58 +0100 Subject: [PATCH 3/3] Always close socket after timeout otherwise we leak open sockets over time --- mcping.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/mcping.js b/mcping.js index d7cfa39..16b9ecd 100644 --- a/mcping.js +++ b/mcping.js @@ -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 @@ -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) => { @@ -128,14 +129,12 @@ function ping_fefd_udp(options, cb) { udp.bind(); - function timeout() { - setTimeout(function(){ - if(state !== 3){ - udp.close;cb('timeout', null, 'fefd_udp'); - } - }, 2000); - } - timeout(); + setTimeout(function(){ + if (state !== DONE){ + cb('timeout', null, 'fefd_udp'); + } + udp.close(); + }, timeout); } function _ping_buffer(options, cb, buffer, type) {