-
Notifications
You must be signed in to change notification settings - Fork 50
Description
Use case
I use partysocket to automatically reconnect a flaky WebSocket. After the tab has been inactive for a while, I want to manually reconnect on window focus, even if the automatic retries have been exhausted.
Expected behavior
I expect that calling ws.reconnect() resets the retry count and attempts a connection. This seems to be the intention based on the code here which resets the retryCount to -1 on .reconnect()
partykit/packages/partysocket/src/ws.ts
Lines 330 to 333 in 09c1d1c
| public reconnect(code?: number, reason?: string) { | |
| this._shouldReconnect = true; | |
| this._closeCalled = false; | |
| this._retryCount = -1; |
Current behavior
Calling ws.reconnect() does nothing after maxRetries has been exhausted.
This is because calling ._connect() sets ._connectLock = true, but in the early return where maxRetries is exhausted it doesn't release the lock.
partykit/packages/partysocket/src/ws.ts
Lines 439 to 453 in 09c1d1c
| private _connect() { | |
| if (this._connectLock || !this._shouldReconnect) { | |
| return; | |
| } | |
| this._connectLock = true; | |
| const { | |
| maxRetries = DEFAULT.maxRetries, | |
| connectionTimeout = DEFAULT.connectionTimeout | |
| } = this._options; | |
| if (this._retryCount >= maxRetries) { | |
| this._debug("max retries reached", this._retryCount, ">=", maxRetries); | |
| return; | |
| } |
I believe the following change could resolve this:
if (this._retryCount >= maxRetries) {
this._debug("max retries reached", this._retryCount, ">=", maxRetries);
+ this._connectLock = false;
return;
}