瀏覽代碼

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
         } else if (status === Strophe.Status.DISCONNECTED) {
288
         } else if (status === Strophe.Status.DISCONNECTED) {
289
             // Stop ping interval
289
             // Stop ping interval
290
             this.connection.ping.stopInterval();
290
             this.connection.ping.stopInterval();
291
-            const wasIntentionalDisconnect = this.disconnectInProgress;
291
+            const wasIntentionalDisconnect = Boolean(this.disconnectInProgress);
292
             const errMsg = msg || this.lastErrorMsg;
292
             const errMsg = msg || this.lastErrorMsg;
293
 
293
 
294
-            this.disconnectInProgress = false;
295
             if (this.anonymousConnectionFailed) {
294
             if (this.anonymousConnectionFailed) {
296
                 // prompt user for username and password
295
                 // prompt user for username and password
297
                 this.eventEmitter.emit(
296
                 this.eventEmitter.emit(
378
         //  Status.DISCONNECTING - The connection is currently being terminated
377
         //  Status.DISCONNECTING - The connection is currently being terminated
379
         //  Status.ATTACHED - The connection has been attached
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
         this.connection.connect(
381
         this.connection.connect(
385
             jid,
382
             jid,
386
             password,
383
             password,
398
      * @param options {object} connecting options - rid, sid, jid and password.
395
      * @param options {object} connecting options - rid, sid, jid and password.
399
      */
396
      */
400
     attach(options) {
397
     attach(options) {
398
+        this._resetState();
401
         const now = this.connectionTimes.attaching = window.performance.now();
399
         const now = this.connectionTimes.attaching = window.performance.now();
402
 
400
 
403
         logger.log('(TIME) Strophe Attaching:\t', now);
401
         logger.log('(TIME) Strophe Attaching:\t', now);
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
      * @param jid
423
      * @param jid
534
      * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
543
      * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
535
      */
544
      */
536
     disconnect(ev) {
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
             const disconnectListener = (credentials, status) => {
553
             const disconnectListener = (credentials, status) => {
547
                 if (status === Strophe.Status.DISCONNECTED) {
554
                 if (status === Strophe.Status.DISCONNECTED) {
548
                     resolve();
555
                     resolve();
551
             };
558
             };
552
 
559
 
553
             this.eventEmitter.on(XMPPEvents.CONNECTION_STATUS_CHANGED, disconnectListener);
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…
取消
儲存