mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
Prevent ping from hanging if the server never replies. #329
This commit is contained in:
parent
345ab6071d
commit
bb88c1d612
1 changed files with 12 additions and 0 deletions
12
src/ping.js
12
src/ping.js
|
@ -4,6 +4,8 @@ const Client = require('./client')
|
|||
const states = require('./states')
|
||||
const tcpDns = require('./client/tcp_dns')
|
||||
|
||||
const closeTimeout = 30 * 1000
|
||||
|
||||
module.exports = ping
|
||||
|
||||
function ping (options, cb) {
|
||||
|
@ -14,9 +16,11 @@ function ping (options, cb) {
|
|||
const version = mcData.version
|
||||
options.majorVersion = version.majorVersion
|
||||
options.protocolVersion = version.version
|
||||
var closeTimer = null
|
||||
|
||||
const client = new Client(false, version.minecraftVersion)
|
||||
client.on('error', function (err) {
|
||||
clearTimeout(closeTimer);
|
||||
cb(err)
|
||||
})
|
||||
|
||||
|
@ -25,6 +29,7 @@ function ping (options, cb) {
|
|||
const start = Date.now()
|
||||
client.once('ping', function (packet) {
|
||||
data.latency = Date.now() - start
|
||||
clearTimeout(closeTimer);
|
||||
cb(null, data)
|
||||
client.end()
|
||||
})
|
||||
|
@ -45,6 +50,13 @@ function ping (options, cb) {
|
|||
})
|
||||
client.state = states.STATUS
|
||||
})
|
||||
|
||||
// timeout against servers that never reply while keeping
|
||||
// the connection open and alive.
|
||||
closeTimer = setTimeout(function () {
|
||||
client.end()
|
||||
cb(new Error("ETIMEDOUT"))
|
||||
}, closeTimeout)
|
||||
|
||||
tcpDns(client, options)
|
||||
options.connect(client)
|
||||
|
|
Loading…
Reference in a new issue