Browse Source

Reloads the page when focus leaves to dispose MUC room. Adds exponential backoff to focus polling.

master
paweldomas 11 years ago
parent
commit
84a453597c
3 changed files with 69 additions and 24 deletions
  1. 2
    23
      app.js
  2. 52
    1
      moderator.js
  3. 15
    0
      util.js

+ 2
- 23
app.js View File

@@ -219,29 +219,8 @@ function doJoin() {
219 219
         generateRoomName();
220 220
     }
221 221
 
222
-    var elem = $iq({to: config.hosts.focus, type: 'set'});
223
-    elem.c('conference', {
224
-        xmlns: 'http://jitsi.org/protocol/focus',
225
-        room: roomName
226
-    });
227
-    elem.up();
228
-    connection.sendIQ(elem,
229
-        function (result) {
230
-            console.info("Focus replied ", result);
231
-            if ('true' === $(result).find('conference').attr('ready')) {
232
-                doJoinAfterFocus();
233
-            } else {
234
-                console.info("Waiting for the focus...");
235
-                window.setTimeout(
236
-                    function () {
237
-                        doJoin();
238
-                    }, 3000);
239
-            }
240
-        },
241
-        function (error) {
242
-            console.warn(error);
243
-        }
244
-    );
222
+    Moderator.allocateConferenceFocus(
223
+        roomName, doJoinAfterFocus);
245 224
 }
246 225
 
247 226
 function doJoinAfterFocus() {

+ 52
- 1
moderator.js View File

@@ -1,10 +1,13 @@
1
-/* global $, config, connection, Etherpad, Toolbar, VideoLayout */
1
+/* global $, $iq, config, connection, Etherpad, hangUp, Strophe, Toolbar,
2
+   Util, VideoLayout */
2 3
 /**
3 4
  * Contains logic responsible for enabling/disabling functionality available
4 5
  * only to moderator users.
5 6
  */
6 7
 var Moderator = (function (my) {
7 8
 
9
+    var getNextTimeout = Util.createExpBackoffTimer(1000);
10
+
8 11
     my.isModerator = function () {
9 12
         return connection.emuc.isModerator();
10 13
     };
@@ -44,6 +47,54 @@ var Moderator = (function (my) {
44 47
                 Moderator.onModeratorStatusChanged(Moderator.isModerator());
45 48
             }
46 49
         );
50
+
51
+        $(document).bind(
52
+            'left.muc',
53
+            function (event, jid) {
54
+                console.info("Someone left is it focus ? " + jid);
55
+                var resource = Strophe.getResourceFromJid(jid);
56
+                if (resource === 'focus') {
57
+                    console.info(
58
+                        "Focus has left the room - leaving conference");
59
+                    //hangUp();
60
+                    // We'd rather reload to have everything re-initialized
61
+                    location.reload();
62
+                }
63
+            }
64
+        );
65
+    };
66
+
67
+    my.allocateConferenceFocus = function (roomName, callback) {
68
+        var elem = $iq({to: config.hosts.focus, type: 'set'});
69
+        elem.c('conference', {
70
+            xmlns: 'http://jitsi.org/protocol/focus',
71
+            room: roomName
72
+        });
73
+        elem.up();
74
+        connection.sendIQ(elem,
75
+            function (result) {
76
+                if ('true' === $(result).find('conference').attr('ready')) {
77
+                    // Reset timer
78
+                    getNextTimeout(true);
79
+                    callback();
80
+                } else {
81
+                    var waitMs = getNextTimeout();
82
+                    console.info("Waiting for the focus... " + waitMs);
83
+                    window.setTimeout(
84
+                        function () {
85
+                            Moderator.allocateConferenceFocus(roomName, callback);
86
+                        }, waitMs);
87
+                }
88
+            },
89
+            function (error) {
90
+                var waitMs = getNextTimeout();
91
+                console.error("Focus error, retry after " + waitMs, error);
92
+                window.setTimeout(
93
+                    function () {
94
+                        Moderator.allocateConferenceFocus(roomName, callback);
95
+                    }, waitMs);
96
+            }
97
+        );
47 98
     };
48 99
 
49 100
     return my;

+ 15
- 0
util.js View File

@@ -84,5 +84,20 @@ var Util = (function (my) {
84 84
         element.setAttribute("data-container", "body");
85 85
     };
86 86
 
87
+    my.createExpBackoffTimer = function (step) {
88
+        var count = 1;
89
+        return function (reset) {
90
+            // Reset call
91
+            if (reset) {
92
+                count = 1;
93
+                return;
94
+            }
95
+            // Calculate next timeout
96
+            var timeout = Math.pow(2, count - 1);
97
+            count += 1;
98
+            return timeout * step;
99
+        };
100
+    };
101
+
87 102
     return my;
88 103
 }(Util || {}));

Loading…
Cancel
Save