浏览代码

converse-muc: Don't send a join presence at every page reload.

Instead, keep track of the connection state of the room and send a presence
only when the room's connection was disrupted.
JC Brand 8 年之前
父节点
当前提交
ec91175a70
共有 1 个文件被更改,包括 37 次插入5 次删除
  1. 37 5
      src/converse-muc.js

+ 37 - 5
src/converse-muc.js

@@ -825,6 +825,11 @@
                      *      the room.
                      */
                     this.registerHandlers();
+                    if (this.model.get('connection_status') ===  Strophe.Status.CONNECTED) {
+                        // We have restored a chat room from session storage,
+                        // so we don't send out a presence stanza again.
+                        return;
+                    }
                     var stanza = $pres({
                         'from': converse.connection.jid,
                         'to': this.getRoomJIDAndNick(nick)
@@ -833,12 +838,12 @@
                     if (password) {
                         stanza.cnode(Strophe.xmlElement("password", [], password));
                     }
-                    this.model.set('connection_status', Strophe.Status.CONNECTING);
+                    this.model.save('connection_status', Strophe.Status.CONNECTING);
                     return converse.connection.send(stanza);
                 },
 
                 cleanup: function () {
-                    this.model.set('connection_status', Strophe.Status.DISCONNECTED);
+                    this.model.save('connection_status', Strophe.Status.DISCONNECTED);
                     this.removeHandlers();
                 },
 
@@ -1371,7 +1376,7 @@
                         if (notification.reason) {
                             this.showDisconnectMessage(__(___('The reason given is: <em>"%1$s"</em>.'), notification.reason));
                         }
-                        this.model.set('connection_status', Strophe.Status.DISCONNECTED);
+                        this.model.save('connection_status', Strophe.Status.DISCONNECTED);
                         return;
                     }
                     _.each(notification.messages, function (message) {
@@ -1479,7 +1484,7 @@
                      *  (XMLElement) pres: The stanza
                      */
                     if (pres.getAttribute('type') === 'error') {
-                        this.model.set('connection_status', Strophe.Status.DISCONNECTED);
+                        this.model.save('connection_status', Strophe.Status.DISCONNECTED);
                         this.showErrorMessage(pres);
                         return true;
                     } 
@@ -1514,7 +1519,7 @@
                         this.hideSpinner().showStatusMessages(pres);
                     }
                     this.occupantsview.updateOccupantsOnPresence(pres);
-                    this.model.set('connection_status', Strophe.Status.CONNECTED);
+                    this.model.save('connection_status', Strophe.Status.CONNECTED);
                     return true;
                 },
 
@@ -2191,6 +2196,33 @@
                     }
                 }
             });
+
+            var reconnectToChatRooms = function () {
+                /* Upon a reconnection event from converse, join again
+                 * all the open chat rooms.
+                 */
+                converse.chatboxviews.each(function (view) {
+                    if (view.model.get('type') === 'chatroom') {
+                        view.model.save('connection_status', Strophe.Status.DISCONNECTED);
+                        view.join(view.model.get('nick'));
+                    }
+                });
+            };
+            converse.on('reconnected', reconnectToChatRooms);
+
+            var disconnectChatRooms = function () {
+                /* When disconnecting, or reconnecting, mark all chat rooms as
+                 * disconnected, so that they will be properly entered again
+                 * when fetched from session storage.
+                 */
+                converse.chatboxes.each(function (model) {
+                    if (model.get('type') === 'chatroom') {
+                        model.save('connection_status', Strophe.Status.DISCONNECTED);
+                    }
+                });
+            };
+            converse.on('reconnecting', disconnectChatRooms);
+            converse.on('disconnecting', disconnectChatRooms);
         }
     });
 }));