Skip to content

Commit

Permalink
Merge pull request #138 from steveluscher/reset-reconnect-timer-on-co…
Browse files Browse the repository at this point in the history
…nnect

Clear the reconnection timer when the socket reconnects for any reason
  • Loading branch information
mkozjak authored Feb 23, 2023
2 parents 1275bba + c38e94c commit be9c241
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class CommonClient extends EventEmitter
private autoconnect: boolean
private ready: boolean
private reconnect: boolean
private reconnect_timer_id: NodeJS.Timeout
private reconnect_interval: number
private max_reconnects: number
private rest_options: IWSClientAdditionalOptions & NodeWebSocket.ClientOptions
Expand Down Expand Up @@ -83,6 +84,7 @@ export default class CommonClient extends EventEmitter
this.autoconnect = autoconnect
this.ready = false
this.reconnect = reconnect
this.reconnect_timer_id = undefined
this.reconnect_interval = reconnect_interval
this.max_reconnects = max_reconnects
this.rest_options = rest_options
Expand Down Expand Up @@ -294,6 +296,7 @@ export default class CommonClient extends EventEmitter
options: IWSClientAdditionalOptions & NodeWebSocket.ClientOptions
)
{
clearTimeout(this.reconnect_timer_id)
this.socket = this.webSocketFactory(address, options)

this.socket.addEventListener("open", () =>
Expand Down Expand Up @@ -382,7 +385,10 @@ export default class CommonClient extends EventEmitter

if (this.reconnect && ((this.max_reconnects > this.current_reconnects) ||
this.max_reconnects === 0))
setTimeout(() => this._connect(address, options), this.reconnect_interval)
this.reconnect_timer_id = setTimeout(
() => this._connect(address, options),
this.reconnect_interval
)
})
}
}

0 comments on commit be9c241

Please sign in to comment.