diff --git a/src/utils/signaling.js b/src/utils/signaling.js index ed09af42911..5ab0f90dbeb 100644 --- a/src/utils/signaling.js +++ b/src/utils/signaling.js @@ -140,6 +140,20 @@ Signaling.Base.prototype._trigger = function(ev, args) { EventBus.$emit('signaling-' + kebabCase(ev), args) } +Signaling.Base.prototype.setSettings = function(settings) { + if (!settings) { + // Signaling object is expected to always have a settings object + return + } + + this.settings = settings + + if (this._pendingUpdateSettingsPromise) { + this._pendingUpdateSettingsPromise.resolve() + delete this._pendingUpdateSettingsPromise + } +} + Signaling.Base.prototype.isNoMcuWarningEnabled = function() { return !this.settings.hideWarning } @@ -646,6 +660,19 @@ Signaling.Standalone.prototype.connect = function() { }, 2000) } + if (this._pendingUpdateSettingsPromise) { + console.info('Deferring establishing signaling connection until signaling settings are updated') + + this._pendingUpdateSettingsPromise.then(() => { + // "reconnect()" is called instead of "connect()", even if that + // slightly delays the connection, as "reconnect()" prevents + // duplicated connection requests. + this.reconnect() + }) + + return + } + console.debug('Connecting to ' + this.url + ' for ' + this.settings.token) this.callbacks = {} this.id = 1 @@ -764,8 +791,7 @@ Signaling.Standalone.prototype.connect = function() { console.error('An error occurred processing the signaling message, please ask your server administrator to check the log file') break case 'token_expired': - console.info('The signaling token is expired, need to update settings') - this._trigger('updateSettings') + this.processErrorTokenExpired() break default: console.error('Ignore unknown error', data) @@ -1351,6 +1377,23 @@ Signaling.Standalone.prototype.processRoomParticipantsEvent = function(data) { } } +Signaling.Standalone.prototype.processErrorTokenExpired = function() { + console.info('The signaling token is expired, need to update settings') + + if (!this._pendingUpdateSettingsPromise) { + let pendingUpdateSettingsPromiseResolve + this._pendingUpdateSettingsPromise = new Promise((resolve, reject) => { + // The Promise executor is run even before the Promise constructor has + // finished, so "this._pendingUpdateSettingsPromise" is not available + // yet. + pendingUpdateSettingsPromiseResolve = resolve + }) + this._pendingUpdateSettingsPromise.resolve = pendingUpdateSettingsPromiseResolve + } + + this._trigger('updateSettings') +} + Signaling.Standalone.prototype.requestOffer = function(sessionid, roomType, sid = undefined) { if (!this.hasFeature('mcu')) { console.warn("Can't request an offer without a MCU.") diff --git a/src/utils/webrtc/index.js b/src/utils/webrtc/index.js index 8be650f7610..0a32cde2494 100644 --- a/src/utils/webrtc/index.js +++ b/src/utils/webrtc/index.js @@ -104,7 +104,7 @@ async function connectSignaling(token) { signaling.on('updateSettings', async function() { const settings = await getSignalingSettings(token) console.debug('Received updated settings', settings) - signaling.settings = settings + signaling.setSettings(settings) }) }