I am trying to setup a custom keep alive every 5s and attach it to the socket object so that I can reset the timer if there is a communication issue. I think it's more a scoping issue than anything else, but it is a roadblock for me.
client.on('connect', function handleConnection() {
this.fullDataDec = ''
this.pingInterval = null
const credentials = {}
credentials.player_id = 123
this.credentials = credentials
const initPacket = buildPacket(this)
this.write(initPacket)
this.pingInterval = setInterval(() => {
const keepAlivePacket = packetBuilder.buildKeepAlivePacket(this)
this.write(keepAlivePacket)
}, 5000)
})
My questions:
this
inside of the setInterval refers to theTimeout
object. How would I pass my socket (the client object) into this function?- Is it bad practice to pass meta information (like credentials) by adding them as property to the socket object? I thought it's the easiest way especially when I plan to run many sockets.