mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
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:
parent
8bfbb72e5b
commit
8f0ae218b1
2 changed files with 9 additions and 1 deletions
|
@ -82,6 +82,7 @@ Returns a `Client` instance and perform login.
|
|||
* sessionServer : session server, default to https://sessionserver.mojang.com
|
||||
* 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`
|
||||
* 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.
|
||||
* 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
|
||||
|
|
|
@ -15,7 +15,8 @@ function ping (options, cb) {
|
|||
options.majorVersion = version.majorVersion
|
||||
options.protocolVersion = version.version
|
||||
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)
|
||||
client.on('error', function (err) {
|
||||
|
@ -26,8 +27,14 @@ function ping (options, cb) {
|
|||
client.once('server_info', function (packet) {
|
||||
const data = JSON.parse(packet.response)
|
||||
const start = Date.now()
|
||||
const maxTime = setTimeout(() => {
|
||||
clearTimeout(closeTimer)
|
||||
cb(null, data)
|
||||
client.end()
|
||||
}, options.noPongTimeout)
|
||||
client.once('ping', function (packet) {
|
||||
data.latency = Date.now() - start
|
||||
clearTimeout(maxTime)
|
||||
clearTimeout(closeTimer)
|
||||
cb(null, data)
|
||||
client.end()
|
||||
|
|
Loading…
Reference in a new issue