Skip to content

Commit 6954cf5

Browse files
Add log about browser tab sleeping (#34008)
1 parent 4a1ced1 commit 6954cf5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/SignalR/clients/ts/signalr/src/HubConnection.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ILogger, LogLevel } from "./ILogger";
88
import { IRetryPolicy } from "./IRetryPolicy";
99
import { IStreamResult } from "./Stream";
1010
import { Subject } from "./Subject";
11-
import { Arg, getErrorString } from "./Utils";
11+
import { Arg, getErrorString, Platform } from "./Utils";
1212

1313
const DEFAULT_TIMEOUT_IN_MS: number = 30 * 1000;
1414
const DEFAULT_PING_INTERVAL_IN_MS: number = 15 * 1000;
@@ -65,6 +65,11 @@ export class HubConnection {
6565
private _timeoutHandle?: any;
6666
private _pingServerHandle?: any;
6767

68+
private _freezeEventListener = () =>
69+
{
70+
this._logger.log(LogLevel.Warning, "The page is being frozen, this will likely lead to the connection being closed and messages being lost. For more information see the docs at https://docs.microsoft.com/aspnet/core/signalr/javascript-client#bsleep");
71+
};
72+
6873
/** The server timeout in milliseconds.
6974
*
7075
* If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error.
@@ -174,6 +179,13 @@ export class HubConnection {
174179
try {
175180
await this._startInternal();
176181

182+
if (Platform.isBrowser) {
183+
if (document) {
184+
// Log when the browser freezes the tab so users know why their connection unexpectedly stopped working
185+
document.addEventListener("freeze", this._freezeEventListener);
186+
}
187+
}
188+
177189
this._connectionState = HubConnectionState.Connected;
178190
this._connectionStarted = true;
179191
this._logger.log(LogLevel.Debug, "HubConnection connected successfully.");
@@ -721,6 +733,12 @@ export class HubConnection {
721733
this._connectionState = HubConnectionState.Disconnected;
722734
this._connectionStarted = false;
723735

736+
if (Platform.isBrowser) {
737+
if (document) {
738+
document.removeEventListener("freeze", this._freezeEventListener);
739+
}
740+
}
741+
724742
try {
725743
this._closedCallbacks.forEach((c) => c.apply(this, [error]));
726744
} catch (e) {

0 commit comments

Comments
 (0)