mirror of
https://github.com/barkeser2002/node-minecraft-protocol.git
synced 2026-09-25 10:35:56 +03:00
* add add client.end() when ping.js fails added reasons in encrypt.js switched order of error event and client.end() in versionChecking.js added an error emitter for keepAliveTimeout * should be an Error object * camalCase socketClosed
26 lines
703 B
JavaScript
26 lines
703 B
JavaScript
'use strict'
|
|
|
|
module.exports = function (client, options) {
|
|
const keepAlive = options.keepAlive == null ? true : options.keepAlive
|
|
if (!keepAlive) return
|
|
|
|
const checkTimeoutInterval = options.checkTimeoutInterval || 30 * 1000
|
|
|
|
client.on('keep_alive', onKeepAlive)
|
|
|
|
let timeout = null
|
|
|
|
client.on('end', () => clearTimeout(timeout))
|
|
|
|
function onKeepAlive (packet) {
|
|
if (timeout) { clearTimeout(timeout) }
|
|
timeout = setTimeout(() => {
|
|
client.emit('error', new Error(`client timed out after ${checkTimeoutInterval} milliseconds`))
|
|
client.end('keepAliveError')
|
|
}, checkTimeoutInterval)
|
|
client.write('keep_alive', {
|
|
keepAliveId: packet.keepAliveId
|
|
})
|
|
}
|
|
}
|