Skip to content

Commit

Permalink
Clear the reconnection timer when the socket reconnects for any reason
Browse files Browse the repository at this point in the history
It's quite possible for a caller to invoke `connect()` on the `Client` while it's in the midst of counting down to a reconnect. This change cancels the reconnection attempt when such an explicit reconnection is ordered.
  • Loading branch information
steveluscher committed Feb 23, 2023
1 parent 1275bba commit c38e94c
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 c38e94c

Please sign in to comment.