瀏覽代碼

fix(xmpp): prevent from calling disconnect twice

Prevents from calling disconnect on the Strophe
connection if it already has been called or
not connected.
master
paweldomas 5 年之前
父節點
當前提交
ac843fd3a2
共有 1 個檔案被更改,包括 23 行新增14 行删除
  1. 23
    14
      modules/xmpp/xmpp.js

+ 23
- 14
modules/xmpp/xmpp.js 查看文件

@@ -288,10 +288,9 @@ export default class XMPP extends Listenable {
288 288
         } else if (status === Strophe.Status.DISCONNECTED) {
289 289
             // Stop ping interval
290 290
             this.connection.ping.stopInterval();
291
-            const wasIntentionalDisconnect = this.disconnectInProgress;
291
+            const wasIntentionalDisconnect = Boolean(this.disconnectInProgress);
292 292
             const errMsg = msg || this.lastErrorMsg;
293 293
 
294
-            this.disconnectInProgress = false;
295 294
             if (this.anonymousConnectionFailed) {
296 295
                 // prompt user for username and password
297 296
                 this.eventEmitter.emit(
@@ -378,9 +377,7 @@ export default class XMPP extends Listenable {
378 377
         //  Status.DISCONNECTING - The connection is currently being terminated
379 378
         //  Status.ATTACHED - The connection has been attached
380 379
 
381
-        this.anonymousConnectionFailed = false;
382
-        this.connectionFailed = false;
383
-        this.lastErrorMsg = undefined;
380
+        this._resetState();
384 381
         this.connection.connect(
385 382
             jid,
386 383
             password,
@@ -398,6 +395,7 @@ export default class XMPP extends Listenable {
398 395
      * @param options {object} connecting options - rid, sid, jid and password.
399 396
      */
400 397
     attach(options) {
398
+        this._resetState();
401 399
         const now = this.connectionTimes.attaching = window.performance.now();
402 400
 
403 401
         logger.log('(TIME) Strophe Attaching:\t', now);
@@ -409,6 +407,17 @@ export default class XMPP extends Listenable {
409 407
             }));
410 408
     }
411 409
 
410
+    /**
411
+     * Resets any state/flag before starting a new connection.
412
+     * @private
413
+     */
414
+    _resetState() {
415
+        this.anonymousConnectionFailed = false;
416
+        this.connectionFailed = false;
417
+        this.lastErrorMsg = undefined;
418
+        this.disconnectInProgress = undefined;
419
+    }
420
+
412 421
     /**
413 422
      *
414 423
      * @param jid
@@ -534,15 +543,13 @@ export default class XMPP extends Listenable {
534 543
      * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
535 544
      */
536 545
     disconnect(ev) {
537
-        if (this.disconnectInProgress || !this.connection) {
538
-            this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
539
-
540
-            return Promise.reject(new Error('Wrong connection state!'));
546
+        if (this.disconnectInProgress) {
547
+            return this.disconnectInProgress;
548
+        } else if (!this.connection) {
549
+            return Promise.resolve();
541 550
         }
542 551
 
543
-        this.disconnectInProgress = true;
544
-
545
-        return new Promise(resolve => {
552
+        this.disconnectInProgress = new Promise(resolve => {
546 553
             const disconnectListener = (credentials, status) => {
547 554
                 if (status === Strophe.Status.DISCONNECTED) {
548 555
                     resolve();
@@ -551,9 +558,11 @@ export default class XMPP extends Listenable {
551 558
             };
552 559
 
553 560
             this.eventEmitter.on(XMPPEvents.CONNECTION_STATUS_CHANGED, disconnectListener);
554
-
555
-            this._cleanupXmppConnection(ev);
556 561
         });
562
+
563
+        this._cleanupXmppConnection(ev);
564
+
565
+        return this.disconnectInProgress;
557 566
     }
558 567
 
559 568
     /**

Loading…
取消
儲存