Skip to content

partysocket: Unable to force .reconnect() after failed connection #252

@mattrossman

Description

@mattrossman

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()

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.

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;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions