From 303f94e8f438a1af11087c27699a72676dbb888c Mon Sep 17 00:00:00 2001 From: SeongHoon Ryu Date: Tue, 29 Mar 2016 00:21:29 +0900 Subject: [PATCH 1/2] Add latency and timeout feature --- mcping.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mcping.js b/mcping.js index f4d7abe..84a8691 100644 --- a/mcping.js +++ b/mcping.js @@ -128,7 +128,21 @@ function ping_fefd_udp(options, cb) { function _ping_buffer(options, cb, buffer, type) { const host = options.host; const port = options.port; + var timeout = 3000; + if (options.timeout) { + timeout = options.timeout; + } + const start = Date.now(); + + const socket = net.connect(port, host); + socket.setTimeout(timeout, function () { + if (callback != undefined) { + callback(new Error("Socket timed out when connecting to " + host + ":" + port), null); + } + socket.end(); + socket.destroy(); + }); let calledCB = false; socket.on('connect', () => { //console.log('connected'); @@ -183,6 +197,7 @@ function _ping_buffer(options, cb, buffer, type) { result.playersOnline = parseInt(parts[1]); result.maxPlayers = parseInt(parts[2]); } + result.latency = Date.now() - start; //console.log('result',result); socket.end(); cb(null, result, type); From 4e1155f2ca6c31675e007cd06c248d92d3dbf212 Mon Sep 17 00:00:00 2001 From: SeongHoon Ryu Date: Tue, 29 Mar 2016 00:32:00 +0900 Subject: [PATCH 2/2] Bug fix --- mcping.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mcping.js b/mcping.js index 84a8691..6a14f14 100644 --- a/mcping.js +++ b/mcping.js @@ -137,8 +137,8 @@ function _ping_buffer(options, cb, buffer, type) { const socket = net.connect(port, host); socket.setTimeout(timeout, function () { - if (callback != undefined) { - callback(new Error("Socket timed out when connecting to " + host + ":" + port), null); + if (cb != undefined) { + cb(new Error("Socket timed out when connecting to " + host + ":" + port), null); } socket.end(); socket.destroy();