Files
usb 86dcbd5f3c add reasons for client.end() & fix issues (#944)
* 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
2021-12-24 18:55:13 +01:00

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
})
}
}