feat: reset ping timeout on any frame recv

Credit to @HUGEIT for suggesting this
This commit is contained in:
Adhiraj Singh
2022-03-24 13:25:23 +05:30
parent 6637e32be9
commit f82f5470cd

View File

@@ -259,6 +259,9 @@ export const makeSocket = ({
const onMessageRecieved = (data: Buffer) => { const onMessageRecieved = (data: Buffer) => {
noise.decodeFrame(data, frame => { noise.decodeFrame(data, frame => {
// reset ping timeout
lastDateRecv = new Date()
ws.emit('frame', frame) ws.emit('frame', frame)
// if it's a binary node // if it's a binary node
if(!(frame instanceof Uint8Array)) { if(!(frame instanceof Uint8Array)) {
@@ -359,7 +362,7 @@ export const makeSocket = ({
end(new Boom('Connection was lost', { statusCode: DisconnectReason.connectionLost })) end(new Boom('Connection was lost', { statusCode: DisconnectReason.connectionLost }))
} else if(ws.readyState === ws.OPEN) { } else if(ws.readyState === ws.OPEN) {
// if its all good, send a keep alive request // if its all good, send a keep alive request
query( sendNode(
{ {
tag: 'iq', tag: 'iq',
attrs: { attrs: {
@@ -369,14 +372,11 @@ export const makeSocket = ({
xmlns: 'w:p', xmlns: 'w:p',
}, },
content: [{ tag: 'ping', attrs: { } }] content: [{ tag: 'ping', attrs: { } }]
}, }
keepAliveIntervalMs
) )
.then(() => { .catch(err => {
lastDateRecv = new Date() logger.error({ trace: err.stack }, 'error in sending keep alive')
logger.trace('recv keep alive')
}) })
.catch(err => end(err))
} else { } else {
logger.warn('keep alive called when WS not open') logger.warn('keep alive called when WS not open')
} }