Skip to content

Commit

Permalink
fix: implement timeout for keepalive requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nepbenonions committed Sep 20, 2022
1 parent 7328b28 commit d59b7b2
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Ember/Socket/S101Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ export default class S101Socket extends EventEmitter {
activeRequest: Request | undefined
status: ConnectionStatus
codec = new S101Codec()
keepaliveResponseWindowTimer: NodeJS.Timer | null

constructor(socket?: Socket) {
super()
this.socket = socket
this.keepaliveIntervalTimer = undefined
this.keepaliveResponseWindowTimer = null
this.activeRequest = undefined
this.status = this.isConnected() ? ConnectionStatus.Connected : ConnectionStatus.Disconnected

this.codec.on('keepaliveReq', () => {
this.sendKeepaliveResponse()
})

this.codec.on('keepaliveResp', () => {
clearInterval(<NodeJS.Timeout>this.keepaliveResponseWindowTimer)
})

this.codec.on('emberPacket', (packet) => {
this.emit('emberPacket', packet)
try {
Expand Down Expand Up @@ -162,6 +168,9 @@ export default class S101Socket extends EventEmitter {
if (this.isConnected() && this.socket) {
try {
this.socket.write(this.codec.keepAliveRequest())
this.keepaliveResponseWindowTimer = setTimeout(() => {
this.handleClose()
}, 500)
} catch (e) {
this.handleClose()
}
Expand Down

0 comments on commit d59b7b2

Please sign in to comment.