Просмотр исходного кода

Merge pull request #561 from jitsi/connection_optimization

Implements server side connection establishment
j8
damencho 9 лет назад
Родитель
Сommit
8a80df2828

+ 3
- 2
Makefile Просмотреть файл

@@ -31,8 +31,9 @@ deploy-appbundle:
31 31
 
32 32
 deploy-lib-jitsi-meet:
33 33
 	cp $(LIBJITSIMEET_DIR)/lib-jitsi-meet.min.js \
34
-	$(LIBJITSIMEET_DIR)/lib-jitsi-meet.min.map $(DEPLOY_DIR)
35
-
34
+	$(LIBJITSIMEET_DIR)/lib-jitsi-meet.min.map \
35
+	$(LIBJITSIMEET_DIR)/connection_optimization/external_connect.js \
36
+	$(DEPLOY_DIR)
36 37
 deploy-css:
37 38
 	(cd css; cat $(CSS_FILES)) | $(CLEANCSS) > css/all.css
38 39
 

+ 22
- 28
app.js Просмотреть файл

@@ -1,4 +1,4 @@
1
-/* global $, JitsiMeetJS, config */
1
+/* global $, JitsiMeetJS, config, getRoomName */
2 2
 /* application specific logic */
3 3
 
4 4
 import "babel-polyfill";
@@ -23,40 +23,34 @@ import API from './modules/API/API';
23 23
 
24 24
 import UIEvents from './service/UI/UIEvents';
25 25
 
26
-
26
+/**
27
+ * Builds and returns the room name.
28
+ */
27 29
 function buildRoomName () {
28
-    let path = window.location.pathname;
29
-    let roomName;
30
-
31
-    // determinde the room node from the url
32
-    // TODO: just the roomnode or the whole bare jid?
33
-    if (config.getroomnode && typeof config.getroomnode === 'function') {
34
-        // custom function might be responsible for doing the pushstate
35
-        roomName = config.getroomnode(path);
36
-    } else {
37
-        /* fall back to default strategy
38
-         * this is making assumptions about how the URL->room mapping happens.
39
-         * It currently assumes deployment at root, with a rewrite like the
40
-         * following one (for nginx):
41
-         location ~ ^/([a-zA-Z0-9]+)$ {
42
-         rewrite ^/(.*)$ / break;
43
-         }
44
-        */
45
-        if (path.length > 1) {
46
-            roomName = path.substr(1).toLowerCase();
47
-        } else {
48
-            let word = RoomnameGenerator.generateRoomWithoutSeparator();
49
-            roomName = word.toLowerCase();
50
-            window.history.pushState(
51
-                'VideoChat', `Room: ${word}`, window.location.pathname + word
52
-            );
53
-        }
30
+    let roomName = getRoomName();
31
+
32
+    if(!roomName) {
33
+        let word = RoomnameGenerator.generateRoomWithoutSeparator();
34
+        roomName = word.toLowerCase();
35
+        window.history.pushState(
36
+            'VideoChat', `Room: ${word}`, window.location.pathname + word
37
+        );
54 38
     }
55 39
 
56 40
     return roomName;
57 41
 }
58 42
 
59 43
 const APP = {
44
+    // Used by do_external_connect.js if we receive the attach data after
45
+    // connect was already executed. status property can be "initialized",
46
+    // "ready" or "connecting". We are interested in "ready" status only which
47
+    // means that connect was executed but we have to wait for the attach data.
48
+    // In status "ready" handler property will be set to a function that will
49
+    // finish the connect process when the attach data or error is received.
50
+    connect: {
51
+        status: "initialized",
52
+        handler: null
53
+    },
60 54
     UI,
61 55
     settings,
62 56
     conference,

+ 39
- 2
connection.js Просмотреть файл

@@ -5,6 +5,42 @@ import LoginDialog from './modules/UI/authentication/LoginDialog';
5 5
 const ConnectionEvents = JitsiMeetJS.events.connection;
6 6
 const ConnectionErrors = JitsiMeetJS.errors.connection;
7 7
 
8
+/**
9
+ * Checks if we have data to use attach instead of connect. If we have the data
10
+ * executes attach otherwise check if we have to wait for the data. If we have
11
+ * to wait for the attach data we are setting handler to APP.connect.handler
12
+ * which is going to be called when the attach data is received otherwise
13
+ * executes connect.
14
+ *
15
+ * @param {string} [id] user id
16
+ * @param {string} [password] password
17
+ * @param {string} [roomName] the name of the conference.
18
+ */
19
+function checkForAttachParametersAndConnect(id, password, connection) {
20
+    if(window.XMPPAttachInfo){
21
+        APP.connect.status = "connecting";
22
+        // When connection optimization is not deployed or enabled the default
23
+        // value will be window.XMPPAttachInfo.status = "error"
24
+        // If the connection optimization is deployed and enabled and there is
25
+        // a failure the value will be window.XMPPAttachInfo.status = "error" 
26
+        if(window.XMPPAttachInfo.status === "error") {
27
+            connection.connect({id, password});
28
+            return;
29
+        }
30
+
31
+        var attachOptions = window.XMPPAttachInfo.data;
32
+        if(attachOptions) {
33
+            connection.attach(attachOptions);
34
+        } else {
35
+            connection.connect({id, password});
36
+        }
37
+    } else {
38
+        APP.connect.status = "ready";
39
+        APP.connect.handler = checkForAttachParametersAndConnect.bind(null,
40
+            id, password, connection);
41
+    }
42
+}
43
+
8 44
 /**
9 45
  * Try to open connection using provided credentials.
10 46
  * @param {string} [id]
@@ -18,7 +54,8 @@ function connect(id, password, roomName) {
18 54
     let connectionConfig = config;
19 55
 
20 56
     connectionConfig.bosh += '?room=' + roomName;
21
-    let connection = new JitsiMeetJS.JitsiConnection(null, null, config);
57
+    let connection
58
+        = new JitsiMeetJS.JitsiConnection(null, config.token, config);
22 59
 
23 60
     return new Promise(function (resolve, reject) {
24 61
         connection.addEventListener(
@@ -50,7 +87,7 @@ function connect(id, password, roomName) {
50 87
             reject(err);
51 88
         }
52 89
 
53
-        connection.connect({id, password});
90
+        checkForAttachParametersAndConnect(id, password, connection);
54 91
     });
55 92
 }
56 93
 

+ 0
- 0
connection_optimization/connection_optimization.html Просмотреть файл


+ 80
- 0
connection_optimization/do_external_connect.js Просмотреть файл

@@ -0,0 +1,80 @@
1
+/* global config, getRoomName, getConfigParamsFromUrl */
2
+/* global createConnectionExternally */
3
+/**
4
+ * Implements extrnal connect using createConnectionExtenally function defined
5
+ * in external_connect.js for Jitsi Meet. Parses the room name and token from
6
+ * the url and executes createConnectionExtenally.
7
+ *
8
+ * NOTE: If you are using lib-jitsi-meet without Jitsi Meet you should use this
9
+ * file as reference only because the implementation is Jitsi Meet specific.
10
+ *
11
+ * NOTE: For optimal results this file should be included right after
12
+ * exrnal_connect.js.
13
+ */
14
+
15
+
16
+
17
+ /**
18
+  * Gets the token from the URL.
19
+  */
20
+function buildToken(){
21
+    var params = getConfigParamsFromUrl();
22
+    return params["config.token"] || config.token;
23
+}
24
+
25
+/**
26
+ * Executes createConnectionExternally function.
27
+ */
28
+(function () {
29
+    // FIXME: Add implementation for changing that config from the url for
30
+    // consistency
31
+    var url = config.externalConnectUrl;
32
+
33
+    /**
34
+     * Check if connect from connection.js was executed and executes the handler
35
+     * that is going to finish the connect work.
36
+     */
37
+    function checkForConnectHandlerAndConnect() {
38
+
39
+        if(window.APP && window.APP.connect.status === "ready") {
40
+            window.APP.connect.handler();
41
+        }
42
+    }
43
+
44
+    function error_callback(error){
45
+        if(error) //error=undefined if external connect is disabled.
46
+            console.warn(error);
47
+        // Sets that global variable to be used later by connect method in
48
+        // connection.js
49
+        window.XMPPAttachInfo = {
50
+            status: "error"
51
+        };
52
+        checkForConnectHandlerAndConnect();
53
+    }
54
+
55
+    if(!url || !window.createConnectionExternally) {
56
+        error_callback();
57
+        return;
58
+    }
59
+    var room_name = getRoomName();
60
+    if(!room_name) {
61
+        error_callback();
62
+        return;
63
+    }
64
+
65
+    url += "?room=" + room_name;
66
+
67
+    var token = buildToken();
68
+    if(token)
69
+        url += "&token=" + token;
70
+
71
+    createConnectionExternally(url, function(connectionInfo) {
72
+        // Sets that global variable to be used later by connect method in
73
+        // connection.js
74
+        window.XMPPAttachInfo = {
75
+            status: "success",
76
+            data: connectionInfo
77
+        };
78
+        checkForConnectHandlerAndConnect();
79
+    }, error_callback);
80
+})();

+ 1
- 0
debian/jitsi-meet.install Просмотреть файл

@@ -9,3 +9,4 @@ sounds					/usr/share/jitsi-meet/
9 9
 fonts					/usr/share/jitsi-meet/
10 10
 images					/usr/share/jitsi-meet/
11 11
 lang					/usr/share/jitsi-meet/
12
+connection_optimization	/usr/share/jitsi-meet/

+ 9
- 6
index.html Просмотреть файл

@@ -1,6 +1,14 @@
1 1
 <html itemscope itemtype="http://schema.org/Product" prefix="og: http://ogp.me/ns#" xmlns="http://www.w3.org/1999/html">
2 2
   <head>
3 3
     <!--#include virtual="title.html" -->
4
+    <script>console.log("(TIME) index.html loaded:\t", window.performance.now());</script>
5
+    <script src="config.js?v=15"></script><!-- adapt to your needs, i.e. set hosts and bosh path -->
6
+    <script src="utils.js?v=1"></script>
7
+    <!--#include virtual="connection_optimization/connection_optimization.html" -->
8
+    <script src="connection_optimization/do_external_connect.js?v=1"></script>
9
+    <script src="interface_config.js?v=6"></script>
10
+    <script src="libs/lib-jitsi-meet.min.js?v=139"></script>
11
+    <script src="libs/app.bundle.min.js?v=139"></script>
4 12
     <link rel="icon" type="image/png" href="/images/favicon.ico?v=1"/>
5 13
     <meta property="og:title" content="Jitsi Meet"/>
6 14
     <meta property="og:image" content="/images/jitsilogo.png?v=1"/>
@@ -9,12 +17,7 @@
9 17
     <meta itemprop="name" content="Jitsi Meet"/>
10 18
     <meta itemprop="description" content="Join a WebRTC video conference powered by the Jitsi Videobridge"/>
11 19
     <meta itemprop="image" content="/images/jitsilogo.png?v=1"/>
12
-      <link rel="stylesheet" href="css/all.css"/>
13
-    <script>console.log("(TIME) index.html loaded:\t", window.performance.now());</script>
14
-    <script src="config.js?v=15"></script><!-- adapt to your needs, i.e. set hosts and bosh path -->
15
-    <script src="interface_config.js?v=6"></script>
16
-    <script src="libs/lib-jitsi-meet.min.js?v=139"></script>
17
-    <script src="libs/app.bundle.min.js?v=139"></script>
20
+    <link rel="stylesheet" href="css/all.css"/>
18 21
     <!--#include virtual="plugin.head.html" -->
19 22
   </head>
20 23
   <body>

+ 2
- 14
modules/config/URLProcessor.js Просмотреть файл

@@ -1,18 +1,6 @@
1
-/* global $, $iq, config, interfaceConfig */
1
+/* global $, $iq, config, interfaceConfig, getConfigParamsFromUrl */
2 2
 var configUtils = require('./Util');
3 3
 var params = {};
4
-function getConfigParamsFromUrl() {
5
-    if (!location.hash)
6
-        return {};
7
-    var hash = location.hash.substr(1);
8
-    var result = {};
9
-    hash.split("&").forEach(function (part) {
10
-        var item = part.split("=");
11
-        result[item[0]] = JSON.parse(
12
-            decodeURIComponent(item[1]).replace(/\\&/, "&"));
13
-    });
14
-    return result;
15
-}
16 4
 
17 5
 params = getConfigParamsFromUrl();
18 6
 
@@ -62,4 +50,4 @@ var URLProcessor = {
62 50
     }
63 51
 };
64 52
 
65
-module.exports = URLProcessor;
53
+module.exports = URLProcessor;

+ 52
- 0
utils.js Просмотреть файл

@@ -0,0 +1,52 @@
1
+/* global config */
2
+
3
+/**
4
+ * Defines some utility methods that are used before the other JS files are
5
+ * loaded.
6
+ */
7
+
8
+
9
+/**
10
+ * Builds and returns the room name.
11
+ */
12
+function getRoomName () {
13
+    var path = window.location.pathname;
14
+    var roomName;
15
+
16
+    // determinde the room node from the url
17
+    // TODO: just the roomnode or the whole bare jid?
18
+    if (config.getroomnode && typeof config.getroomnode === 'function') {
19
+        // custom function might be responsible for doing the pushstate
20
+        roomName = config.getroomnode(path);
21
+    } else {
22
+        /* fall back to default strategy
23
+         * this is making assumptions about how the URL->room mapping happens.
24
+         * It currently assumes deployment at root, with a rewrite like the
25
+         * following one (for nginx):
26
+         location ~ ^/([a-zA-Z0-9]+)$ {
27
+         rewrite ^/(.*)$ / break;
28
+         }
29
+        */
30
+        if (path.length > 1) {
31
+            roomName = path.substr(1).toLowerCase();
32
+        }
33
+    }
34
+
35
+    return roomName;
36
+}
37
+
38
+/**
39
+ * Parses the hash parameters from the URL and returns them as a JS object.
40
+ */
41
+function getConfigParamsFromUrl() {
42
+    if (!location.hash)
43
+        return {};
44
+    var hash = location.hash.substr(1);
45
+    var result = {};
46
+    hash.split("&").forEach(function (part) {
47
+        var item = part.split("=");
48
+        result[item[0]] = JSON.parse(
49
+            decodeURIComponent(item[1]).replace(/\\&/, "&"));
50
+    });
51
+    return result;
52
+}

Загрузка…
Отмена
Сохранить