Merge in 'release/5.0' changes
This commit is contained in:
commit
dae24296a2
|
|
@ -54,6 +54,7 @@ export class HubConnection {
|
||||||
private connectionStarted: boolean;
|
private connectionStarted: boolean;
|
||||||
private startPromise?: Promise<void>;
|
private startPromise?: Promise<void>;
|
||||||
private stopPromise?: Promise<void>;
|
private stopPromise?: Promise<void>;
|
||||||
|
private nextKeepAlive: number = 0;
|
||||||
|
|
||||||
// The type of these a) doesn't matter and b) varies when building in browser and node contexts
|
// The type of these a) doesn't matter and b) varies when building in browser and node contexts
|
||||||
// Since we're building the WebPack bundle directly from the TypeScript, this matters (previously
|
// Since we're building the WebPack bundle directly from the TypeScript, this matters (previously
|
||||||
|
|
@ -73,6 +74,8 @@ export class HubConnection {
|
||||||
*
|
*
|
||||||
* The default value is 15,000 milliseconds (15 seconds).
|
* The default value is 15,000 milliseconds (15 seconds).
|
||||||
* Allows the server to detect hard disconnects (like when a client unplugs their computer).
|
* Allows the server to detect hard disconnects (like when a client unplugs their computer).
|
||||||
|
* The ping will happen at most as often as the server pings.
|
||||||
|
* If the server pings every 5 seconds, a value lower than 5 will ping every 5 seconds.
|
||||||
*/
|
*/
|
||||||
public keepAliveIntervalInMilliseconds: number;
|
public keepAliveIntervalInMilliseconds: number;
|
||||||
|
|
||||||
|
|
@ -603,7 +606,26 @@ export class HubConnection {
|
||||||
return;
|
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();
|
this.cleanupPingTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetTimeoutPeriod() {
|
||||||
|
if (!this.connection.features || !this.connection.features.inherentKeepAlive) {
|
||||||
|
// Set the timeout timer
|
||||||
|
this.timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);
|
||||||
|
|
||||||
|
// Set keepAlive timer if there isn't one
|
||||||
|
if (this.pingServerHandle === undefined) {
|
||||||
|
let nextPing = this.nextKeepAlive - new Date().getTime();
|
||||||
|
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 () => {
|
this.pingServerHandle = setTimeout(async () => {
|
||||||
if (this.connectionState === HubConnectionState.Connected) {
|
if (this.connectionState === HubConnectionState.Connected) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -614,13 +636,8 @@ export class HubConnection {
|
||||||
this.cleanupPingTimer();
|
this.cleanupPingTimer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, this.keepAliveIntervalInMilliseconds);
|
}, nextPing);
|
||||||
}
|
}
|
||||||
|
|
||||||
private resetTimeoutPeriod() {
|
|
||||||
if (!this.connection.features || !this.connection.features.inherentKeepAlive) {
|
|
||||||
// Set the timeout timer
|
|
||||||
this.timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -811,6 +828,7 @@ export class HubConnection {
|
||||||
private cleanupPingTimer(): void {
|
private cleanupPingTimer(): void {
|
||||||
if (this.pingServerHandle) {
|
if (this.pingServerHandle) {
|
||||||
clearTimeout(this.pingServerHandle);
|
clearTimeout(this.pingServerHandle);
|
||||||
|
this.pingServerHandle = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,7 @@ describe("HubConnection", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("ping", () => {
|
describe("ping", () => {
|
||||||
it("automatically sends multiple pings", async () => {
|
it("sends pings when receiving pings", async () => {
|
||||||
await VerifyLogger.run(async (logger) => {
|
await VerifyLogger.run(async (logger) => {
|
||||||
const connection = new TestConnection();
|
const connection = new TestConnection();
|
||||||
const hubConnection = createHubConnection(connection, logger);
|
const hubConnection = createHubConnection(connection, logger);
|
||||||
|
|
@ -118,8 +118,15 @@ describe("HubConnection", () => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await hubConnection.start();
|
await hubConnection.start();
|
||||||
|
|
||||||
|
const pingInterval = setInterval(async () => {
|
||||||
|
await connection.receive({ type: MessageType.Ping });
|
||||||
|
}, 5);
|
||||||
|
|
||||||
await delayUntil(500);
|
await delayUntil(500);
|
||||||
|
|
||||||
|
clearInterval(pingInterval);
|
||||||
|
|
||||||
const numPings = connection.sentData.filter((s) => JSON.parse(s).type === MessageType.Ping).length;
|
const numPings = connection.sentData.filter((s) => JSON.parse(s).type === MessageType.Ping).length;
|
||||||
expect(numPings).toBeGreaterThanOrEqual(2);
|
expect(numPings).toBeGreaterThanOrEqual(2);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue