Repository: dotnet/aspnetcore
File: src/SignalR/clients/ts/signalr/src/HubConnection.ts (TypeScript)

Code before:

        this._handshakeResolver();
        return remainingData;
    }

    private _resetKeepAliveInterval() {
        if (this.connection.features.inherentKeepAlive) {
            return;
        }

        // Set the time we want the next keep alive to be sent
        // Timer will be setup on next message receive
        this._nextKeepAlive = new Date().getTime() + this.keepAliveIntervalInMilliseconds;

        this._cleanupPingTimer();
    }

    private _resetTimeoutPeriod() {
        if (!this.connection.features || !this.connection.features.inherentKeepAlive) {
            // Set the timeout timer
            this._timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);
            let nextPing = this._nextKeepAlive - new Date().getTime();
            if (nextPing < 0) {
                if (this._connectionState === HubConnectionState.Connected) {
                    this._sendMessage(this._cachedPingMessage).catch((e) => {
                        this._logger.log(LogLevel.Warning, "Error sending keep-alive message"  + e);
                    });
                    return;
                }
            }

            // Set keepAlive timer if there isn't one
            if (this._pingServerHandle === undefined)
            {
                if (nextPing < 0) {
                    nextPing = 0;
                }

                // The timer needs to be set from a networking callback to avoid Chrome timer throttling from causing timers to run once a minute
                this._pingServerHandle = setTimeout(async () => {
                    if (this._connectionState === HubConnectionState.Connected) {
                        try {
                            await this._sendMessage(this._cachedPingMessage);
                        } catch {
                            // We don't care about the error. It should be seen elsewhere in the client.
                            // The connection is probably in a bad or closed state now, cleanup the timer so it stops triggering
                            this._cleanupPingTimer();
                        }
                    }
                }, nextPing);
            }

Review comment (bug):
Should probably just do the same thing we do in the send below (ignore failure, cleanup timer).

Maybe move to a function so we can share the code?

Code after:

        this._handshakeResolver();
        return remainingData;
    }

    private _resetKeepAliveInterval() {
        if (this.connection.features.inherentKeepAlive) {
            return;
        }

        // Set the time we want the next keep alive to be sent
        // Timer will be setup on next message receive
        this._nextKeepAlive = new Date().getTime() + this.keepAliveIntervalInMilliseconds;

        this._cleanupPingTimer();
    }

    private _resetTimeoutPeriod() {
        if (!this.connection.features || !this.connection.features.inherentKeepAlive) {
            // Set the timeout timer
            this._timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);

            // Immediately fire Keep-Alive ping if nextPing is overdue to avoid dependency on JS timers
            let nextPing = this._nextKeepAlive - new Date().getTime();
            if (nextPing < 0) {
                if (this._connectionState === HubConnectionState.Connected) {
                    // eslint-disable-next-line @typescript-eslint/no-floating-promises
                    this._trySendPingMessage();
                }
                return;
            }

            // Set keepAlive timer if there isn't one
            if (this._pingServerHandle === undefined)
            {
                if (nextPing < 0) {
                    nextPing = 0;
                }

                // The timer needs to be set from a networking callback to avoid Chrome timer throttling from causing timers to run once a minute
                this._pingServerHandle = setTimeout(async () => {
                    if (this._connectionState === HubConnectionState.Connected) {
                        await this._trySendPingMessage();
                    }
                }, nextPing);
            }
        }
    }

    // eslint-disable-next-line @typescript-eslint/naming-convention
    private serverTimeout() {