@@ -721,24 +721,27 @@ export class HubConnection {
721
721
// Set the timeout timer
722
722
this . _timeoutHandle = setTimeout ( ( ) => this . serverTimeout ( ) , this . serverTimeoutInMilliseconds ) ;
723
723
724
+ // Immediately fire Keep-Alive ping if nextPing is overdue to avoid dependency on JS timers
725
+ let nextPing = this . _nextKeepAlive - new Date ( ) . getTime ( ) ;
726
+ if ( nextPing < 0 ) {
727
+ if ( this . _connectionState === HubConnectionState . Connected ) {
728
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
729
+ this . _trySendPingMessage ( ) ;
730
+ }
731
+ return ;
732
+ }
733
+
724
734
// Set keepAlive timer if there isn't one
725
735
if ( this . _pingServerHandle === undefined )
726
736
{
727
- let nextPing = this . _nextKeepAlive - new Date ( ) . getTime ( ) ;
728
737
if ( nextPing < 0 ) {
729
738
nextPing = 0 ;
730
739
}
731
740
732
741
// The timer needs to be set from a networking callback to avoid Chrome timer throttling from causing timers to run once a minute
733
742
this . _pingServerHandle = setTimeout ( async ( ) => {
734
743
if ( this . _connectionState === HubConnectionState . Connected ) {
735
- try {
736
- await this . _sendMessage ( this . _cachedPingMessage ) ;
737
- } catch {
738
- // We don't care about the error. It should be seen elsewhere in the client.
739
- // The connection is probably in a bad or closed state now, cleanup the timer so it stops triggering
740
- this . _cleanupPingTimer ( ) ;
741
- }
744
+ await this . _trySendPingMessage ( ) ;
742
745
}
743
746
} , nextPing ) ;
744
747
}
@@ -1149,4 +1152,14 @@ export class HubConnection {
1149
1152
private _createCloseMessage ( ) : CloseMessage {
1150
1153
return { type : MessageType . Close } ;
1151
1154
}
1155
+
1156
+ private async _trySendPingMessage ( ) : Promise < void > {
1157
+ try {
1158
+ await this . _sendMessage ( this . _cachedPingMessage ) ;
1159
+ } catch {
1160
+ // We don't care about the error. It should be seen elsewhere in the client.
1161
+ // The connection is probably in a bad or closed state now, cleanup the timer so it stops triggering
1162
+ this . _cleanupPingTimer ( ) ;
1163
+ }
1164
+ }
1152
1165
}
0 commit comments