Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement timeout for keepalive requests #25

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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