add options.noPongTimeout to handle server that don't answer to ping packet

useful for example for minersbr.enxada.host in auto version mode
This commit is contained in:
Romain Beaumont 2020-10-25 15:14:22 +00:00
parent 8bfbb72e5b
commit 8f0ae218b1
2 changed files with 9 additions and 1 deletions

View file

@ -82,6 +82,7 @@ Returns a `Client` instance and perform login.
* sessionServer : session server, default to https://sessionserver.mojang.com * sessionServer : session server, default to https://sessionserver.mojang.com
* keepAlive : send keep alive packets : default to true * keepAlive : send keep alive packets : default to true
* closeTimeout : end the connection after this delay in milliseconds if server doesn't answer to ping, default to `120*1000` * closeTimeout : end the connection after this delay in milliseconds if server doesn't answer to ping, default to `120*1000`
* noPongTimeout : after the server opened the connection, wait for a default of `5*1000` after pinging and answers without the latency
* checkTimeoutInterval : default to `30*1000` (30s), check if keepalive received at that period, disconnect otherwise. * checkTimeoutInterval : default to `30*1000` (30s), check if keepalive received at that period, disconnect otherwise.
* version : 1.8 or 1.9 or false (to auto-negotiate): default to 1.8 * version : 1.8 or 1.9 or false (to auto-negotiate): default to 1.8
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example * customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example

View file

@ -15,7 +15,8 @@ function ping (options, cb) {
options.majorVersion = version.majorVersion options.majorVersion = version.majorVersion
options.protocolVersion = version.version options.protocolVersion = version.version
var closeTimer = null var closeTimer = null
options.closeTimeout = 120 * 1000 options.closeTimeout = options.closeTimeout || 120 * 1000
options.noPongTimeout = options.noPongTimeout || 5 * 1000
const client = new Client(false, version.minecraftVersion) const client = new Client(false, version.minecraftVersion)
client.on('error', function (err) { client.on('error', function (err) {
@ -26,8 +27,14 @@ function ping (options, cb) {
client.once('server_info', function (packet) { client.once('server_info', function (packet) {
const data = JSON.parse(packet.response) const data = JSON.parse(packet.response)
const start = Date.now() const start = Date.now()
const maxTime = setTimeout(() => {
clearTimeout(closeTimer)
cb(null, data)
client.end()
}, options.noPongTimeout)
client.once('ping', function (packet) { client.once('ping', function (packet) {
data.latency = Date.now() - start data.latency = Date.now() - start
clearTimeout(maxTime)
clearTimeout(closeTimer) clearTimeout(closeTimer)
cb(null, data) cb(null, data)
client.end() client.end()