From d9a2f120c3a9afc79b2c9e8d83b59708628d9fe2 Mon Sep 17 00:00:00 2001 From: Martin Schoeler Date: Mon, 23 May 2022 15:34:48 -0300 Subject: [PATCH] Chore: Code Improvements for #25391 (#25606) --- apps/meteor/client/lib/voip/VoIPUser.ts | 46 ++++++++----------- .../src/voip/VoIPUserConfiguration.ts | 2 +- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/apps/meteor/client/lib/voip/VoIPUser.ts b/apps/meteor/client/lib/voip/VoIPUser.ts index e8628b4efbd87..692a6dd4a6ca7 100644 --- a/apps/meteor/client/lib/voip/VoIPUser.ts +++ b/apps/meteor/client/lib/voip/VoIPUser.ts @@ -4,7 +4,7 @@ * This class encapsulates all the details of sip.js and exposes * a very simple functions and callback handlers to the outside world. * This class thus abstracts user from Browser specific media details as well as - * SIP specific protol details. + * SIP specific protocol details. */ import { CallStates, @@ -144,7 +144,7 @@ export class VoIPUser extends Emitter { this.userAgent.transport.isConnected(); this._opInProgress = Operation.OP_CONNECT; try { - this.registerer = new Registerer(this.userAgent /* , { expires: 60 }*/); + this.registerer = new Registerer(this.userAgent); this.userAgent.transport.onConnect = this.onConnected.bind(this); this.userAgent.transport.onDisconnect = this.onDisconnected.bind(this); @@ -204,7 +204,7 @@ export class VoIPUser extends Emitter { * because after the network recovery and after reconnecting to the server, * the transport layer of SIPUA does not call onConnected. So by passing |checkRegistration = true | * the code will check if the endpoint was previously registered before the disconnection. - * If such is the case, it will first unregister and then reregister. + * If such is the case, it will first unregister and then re-register. * */ this.attemptReconnection(); if (this.registerer && this.callState !== 'INITIAL') { @@ -386,7 +386,7 @@ export class VoIPUser extends Emitter { const remoteStream = sdh.remoteMediaStream; if (!remoteStream) { - throw new Error('Remote media stream undefiend.'); + throw new Error('Remote media stream is undefined.'); } this.remoteStream = new Stream(remoteStream); @@ -593,7 +593,7 @@ export class VoIPUser extends Emitter { // Call state must be in offer_received. if (this._callState === 'OFFER_RECEIVED' && this._opInProgress === Operation.OP_PROCESS_INVITE) { this._callState = 'ANSWER_SENT'; - // Somethingis wrong, this session is not an instance of INVITE + // Something is wrong, this session is not an instance of INVITE if (!(this.session instanceof Invitation)) { throw new Error('Session not instance of Invitation.'); } @@ -631,7 +631,7 @@ export class VoIPUser extends Emitter { return this.session.accept(invitationAcceptOptions); } - throw new Error('Something went wront'); + throw new Error('Something went wrong'); } /** @@ -787,19 +787,19 @@ export class VoIPUser extends Emitter { /** * Connection is lost in 3 ways - * 1. When local network is lost (Router is disconeected, switching networks, devtools->network->offline) + * 1. When local network is lost (Router is disconnected, switching networks, devtools->network->offline) * In this case, the SIP.js's transport layer does not detect the disconnection. Hence, it does not * call |onDisconnect|. To detect this kind of disconnection, window event listeners have been added. * These event listeners would be get called when the browser detects that network is offline or online. * When the network is restored, the code tries to reconnect. The useragent.transport "does not" generate the * onconnected event in this case as well. so onlineNetworkHandler calls attemptReconnection. - * Which calls attemptRegistrationPostRecovery based on correct state. attemptRegistrationPostRecovery firts tries to - * unregister and then reregister. + * Which calls attemptRegistrationPostRecovery based on correct state. attemptRegistrationPostRecovery first tries to + * unregister and then re-register. * Important note : We use the event listeners using bind function object offlineNetworkHandler and onlineNetworkHandler * It is done so because the same event handlers need to be used for removeEventListener, which becomes impossible * if done inline. * - * 2. Computer goes to sleep. In this case onDisconnect is triggerred. The code tries to reconnect but cant go ahead + * 2. Computer goes to sleep. In this case onDisconnect is triggered. The code tries to reconnect but cant go ahead * as it goes to sleep. On waking up, The attemptReconnection gets executed, connection is completed. * In this case, it generates onConnected event. In this onConnected event it calls attemptRegistrationPostRecovery * @@ -808,7 +808,7 @@ export class VoIPUser extends Emitter { * * Retry count : * connectionRetryCount is the parameter called |Retry Count| in - * Adminstration -> Call Center -> Server configuration -> Retry count. + * Administration -> Call Center -> Server configuration -> Retry count. * The retry is implemented with backoff, maxbackoff = 8 seconds. * For continuous retries (In case Asterisk restart happens) Set this parameter to -1. * @@ -876,7 +876,7 @@ export class VoIPUser extends Emitter { } async sendKeepAliveAndWaitForResponse(withDebounce = false): Promise { - const promise = new Promise((resolve) => { + const promise = new Promise((resolve, reject) => { let keepAliveAccepted = false; let responseWaitTime = this.optionsKeepaliveInterval / 2; if (withDebounce) { @@ -893,7 +893,7 @@ export class VoIPUser extends Emitter { }); setTimeout(async () => { if (!keepAliveAccepted) { - resolve(false); + reject(false); } else { if (this.attemptRegistration) { this.attemptPostRecoveryRoutine(); @@ -912,22 +912,14 @@ export class VoIPUser extends Emitter { return; } if (this._connectionState !== 'SERVER_RECONNECTING') { - let keepAliveResponse = await this.sendKeepAliveAndWaitForResponse(); + const keepAliveResponse = await this.sendKeepAliveAndWaitForResponse(); if (!keepAliveResponse) { - const connectivityArray = []; - for (let i = 0; i < this.optionsKeepAliveDebounceCount; i++) { - connectivityArray.push(this.sendKeepAliveAndWaitForResponse(true)); - } - const values = await Promise.all(connectivityArray); - for (const i in values) { - if (values[i]) { - keepAliveResponse = values[i]; - break; + const connectivityArray = new Array(this.optionsKeepAliveDebounceCount).fill(this.sendKeepAliveAndWaitForResponse(true)); + await Promise.race(connectivityArray).then((response): void => { + if (!response) { + this.networkEmitter.emit('disconnected'); } - } - if (!keepAliveResponse) { - this.networkEmitter.emit('disconnected'); - } + }); } /** * Either we got connected and managed to send keep-alive diff --git a/packages/core-typings/src/voip/VoIPUserConfiguration.ts b/packages/core-typings/src/voip/VoIPUserConfiguration.ts index 48f9d55cc6f2a..ff2b60e7a5433 100644 --- a/packages/core-typings/src/voip/VoIPUserConfiguration.ts +++ b/packages/core-typings/src/voip/VoIPUserConfiguration.ts @@ -41,7 +41,7 @@ export interface VoIPUserConfiguration { */ connectionRetryCount: number; /** - * Voip Retry count + * Enable Keep Alive for unstable networks * @defaultValue undefined */ enableKeepAliveUsingOptionsForUnstableNetworks: boolean;