Skip to content

Commit 80df84a

Browse files
committed
fix: Fixes sending initial keep-alive.
1 parent 916ebe5 commit 80df84a

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

modules/xmpp/XmppConnection.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,16 @@ export default class XmppConnection extends Listenable {
405405
if (this._usesWebsocket) {
406406
if (this._oneSuccessfulConnect) {
407407
// on reconnect we do it immediately
408-
this._keepAliveAndCheckShard();
408+
this._maybeStartWSKeepAlive(0);
409409
} else {
410410
// delay it a bit to not interfere with the connection process
411411
// and to allow backend to correct any possible split brain issues
412412
// Store timeout so it can be cleared if needed
413-
this._wsKeepAlive = setTimeout(() => this._keepAliveAndCheckShard(), 5000);
413+
this._maybeStartWSKeepAlive(5000);
414414
}
415415
}
416416
this._oneSuccessfulConnect = true;
417417

418-
this._maybeStartWSKeepAlive();
419418
this._processDeferredIQs();
420419
this._resumeTask.cancel();
421420
this.ping.startInterval(this._options.pingOptions?.domain || this.domain);
@@ -526,24 +525,29 @@ export default class XmppConnection extends Listenable {
526525
/**
527526
* Starts the Websocket keep alive if enabled.
528527
*
528+
* @param {number|undefined} forcedTimeout - If provided, this timeout will be used instead of
529+
* the configured one with added jitter.
530+
*
529531
* @private
530532
* @returns {void}
531533
*/
532-
_maybeStartWSKeepAlive(): void {
534+
_maybeStartWSKeepAlive(forcedTimeout?: number): void {
533535
const { websocketKeepAlive } = this._options;
534536

537+
// if websocketKeepAlive is not set keepAlive is disabled
535538
if (this._usesWebsocket && websocketKeepAlive > 0) {
536539
this._wsKeepAlive || logger.info(`WebSocket keep alive interval: ${websocketKeepAlive}ms`);
537540
clearTimeout(this._wsKeepAlive);
538541

539-
const intervalWithJitter = /* base */ websocketKeepAlive + /* jitter */ (Math.random() * 60 * 1000);
542+
const interval = forcedTimeout
543+
?? (/* base */ websocketKeepAlive + /* jitter */ (Math.random() * 60 * 1000));
540544

541-
logger.debug(`Scheduling next WebSocket keep-alive in ${intervalWithJitter}ms`);
545+
logger.debug(`Scheduling next WebSocket keep-alive in ${interval}ms`);
542546

543547
this._wsKeepAlive = setTimeout(
544548
() => this._keepAliveAndCheckShard()
545549
.then(() => this._maybeStartWSKeepAlive()),
546-
intervalWithJitter);
550+
interval);
547551
}
548552
}
549553

0 commit comments

Comments
 (0)