Browse Source

Implements attach method in JitsiConnection. Fixes token auth.

dev1
hristoterezov 9 years ago
parent
commit
de851ca50e
4 changed files with 158 additions and 77 deletions
  1. 14
    2
      JitsiConnection.js
  2. 50
    0
      connection_optimization/external_connect.js
  3. 2
    4
      modules/xmpp/ChatRoom.js
  4. 92
    71
      modules/xmpp/xmpp.js

+ 14
- 2
JitsiConnection.js View File

13
     this.appID = appID;
13
     this.appID = appID;
14
     this.token = token;
14
     this.token = token;
15
     this.options = options;
15
     this.options = options;
16
-    this.xmpp = new XMPP(options);
16
+    this.xmpp = new XMPP(options, token);
17
     this.conferences = {};
17
     this.conferences = {};
18
 }
18
 }
19
 
19
 
20
 /**
20
 /**
21
  * Connect the client with the server.
21
  * Connect the client with the server.
22
- * @param options {object} connecting options (for example authentications parameters).
22
+ * @param options {object} connecting options
23
+ * (for example authentications parameters).
23
  */
24
  */
24
 JitsiConnection.prototype.connect = function (options) {
25
 JitsiConnection.prototype.connect = function (options) {
25
     if(!options)
26
     if(!options)
28
     this.xmpp.connect(options.id, options.password);
29
     this.xmpp.connect(options.id, options.password);
29
 }
30
 }
30
 
31
 
32
+/**
33
+ * Attach to existing connection. Can be used for optimizations. For example:
34
+ * if the connection is created on the server we can attach to it and start
35
+ * using it.
36
+ *
37
+ * @param options {object} connecting options - rid, sid and jid.
38
+ */
39
+JitsiConnection.prototype.attach = function (options) {
40
+    this.xmpp.attach(options);
41
+}
42
+
31
 /**
43
 /**
32
  * Disconnect the client from the server.
44
  * Disconnect the client from the server.
33
  */
45
  */

+ 50
- 0
connection_optimization/external_connect.js View File

1
+/**
2
+ * Requests the given webservice that will create the connection and will return
3
+ * the necessary details(rid, sid and jid) to attach to this connection and
4
+ * start using it. This script can be used for optimizing the connection startup
5
+ * time. The function will send AJAX request to a webservice that should
6
+ * create the bosh session much faster than the client because the webservice
7
+ * can be started on the same machine as the XMPP serever.
8
+ *
9
+ * NOTE: It's vert important to execute this function as early as you can for
10
+ * optimal results.
11
+ *
12
+ * @param webserviceUrl the url for the web service that is going to create the
13
+ * connection.
14
+ * @param success_callback callback function called with the result of the AJAX
15
+ * request if the request was successfull. The callback will receive one
16
+ * parameter which will be JS Object with properties - rid, sid and jid. This
17
+ * result should be passed to JitsiConnection.attach method in order to use that
18
+ * connection.
19
+ * @param error_callback callback function called the AJAX request fail. This
20
+ * callback is going to receive one parameter which is going to be JS error
21
+ * object with a reason for failure in it.
22
+ */
23
+function createConnectionExternally(webserviceUrl, success_callback,
24
+    error_callback) {
25
+    if (!window.XMLHttpRequest) {
26
+        error_callback(new Error("XMLHttpRequest is not supported!"));
27
+        return;
28
+    }
29
+
30
+    var xhttp = new XMLHttpRequest();
31
+
32
+    xhttp.onreadystatechange = function() {
33
+        if (xhttp.readyState == 4) {
34
+            if (xhttp.status == 200) {
35
+                try {
36
+                    var data = JSON.parse(xhttp.responseText);
37
+                    success_callback(data);
38
+                } catch (e) {
39
+                    error_callback(e);
40
+                }
41
+            } else {
42
+                error_callback(new Error("XMLHttpRequest error. Status: " +
43
+                    xhttp.status + ". Error message: " + xhttp.statusText));
44
+            }
45
+        }
46
+    };
47
+
48
+    xhttp.open("GET", webserviceUrl, true);
49
+    xhttp.send();
50
+}

+ 2
- 4
modules/xmpp/ChatRoom.js View File

117
     if(password)
117
     if(password)
118
         this.password = password;
118
         this.password = password;
119
     var self = this;
119
     var self = this;
120
-    this.moderator.allocateConferenceFocus(function()
121
-    {
122
-        self.sendPresence(true);
123
-    }.bind(this));
120
+    this.moderator.allocateConferenceFocus(function () {});
121
+    self.sendPresence(true);
124
 };
122
 };
125
 
123
 
126
 ChatRoom.prototype.sendPresence = function (fromJoin) {
124
 ChatRoom.prototype.sendPresence = function (fromJoin) {

+ 92
- 71
modules/xmpp/xmpp.js View File

11
 
11
 
12
 var authenticatedUser = false;
12
 var authenticatedUser = false;
13
 
13
 
14
-function createConnection(bosh) {
14
+function createConnection(bosh, token) {
15
     bosh = bosh || '/http-bind';
15
     bosh = bosh || '/http-bind';
16
 
16
 
17
     // Append token as URL param
17
     // Append token as URL param
18
-    if (this.token) {
19
-        bosh += (bosh.indexOf('?') == -1 ? '?' : '&') + 'token=' + this.token;
18
+    if (token) {
19
+        bosh += (bosh.indexOf('?') == -1 ? '?' : '&') + 'token=' + token;
20
     }
20
     }
21
 
21
 
22
     return new Strophe.Connection(bosh);
22
     return new Strophe.Connection(bosh);
33
     require("./strophe.logger")();
33
     require("./strophe.logger")();
34
 }
34
 }
35
 
35
 
36
-function XMPP(options) {
36
+function XMPP(options, token) {
37
     this.eventEmitter = new EventEmitter();
37
     this.eventEmitter = new EventEmitter();
38
     this.connection = null;
38
     this.connection = null;
39
     this.disconnectInProgress = false;
39
     this.disconnectInProgress = false;
42
     this.options = options;
42
     this.options = options;
43
     initStrophePlugins(this);
43
     initStrophePlugins(this);
44
 
44
 
45
-    this.connection = createConnection(options.bosh);
45
+    this.connection = createConnection(options.bosh, token);
46
 
46
 
47
     // Setup a disconnect on unload as a way to facilitate API consumers. It
47
     // Setup a disconnect on unload as a way to facilitate API consumers. It
48
     // sounds like they would want that. A problem for them though may be if
48
     // sounds like they would want that. A problem for them though may be if
54
 
54
 
55
 XMPP.prototype.getConnection = function () { return this.connection; };
55
 XMPP.prototype.getConnection = function () { return this.connection; };
56
 
56
 
57
+/**
58
+ * Receive connection status changes and handles them.
59
+ * @password {string} the password passed in connect method
60
+ * @status the connection status
61
+ * @msg message
62
+ */
63
+XMPP.prototype.connectionHandler = function (password, status, msg) {
64
+    logger.log("(TIME) Strophe " + Strophe.getStatusString(status) +
65
+        (msg ? "[" + msg + "]" : "") + "\t:" + window.performance.now());
66
+    if (status === Strophe.Status.CONNECTED ||
67
+        status === Strophe.Status.ATTACHED) {
68
+        if (this.options.useStunTurn) {
69
+            this.connection.jingle.getStunAndTurnCredentials();
70
+        }
71
+
72
+        logger.info("My Jabber ID: " + this.connection.jid);
73
+
74
+        // Schedule ping ?
75
+        var pingJid = this.connection.domain;
76
+        this.connection.ping.hasPingSupport(
77
+            pingJid,
78
+            function (hasPing) {
79
+                if (hasPing)
80
+                    this.connection.ping.startInterval(pingJid);
81
+                else
82
+                    logger.warn("Ping NOT supported by " + pingJid);
83
+            }.bind(this));
84
+
85
+        if (password)
86
+            authenticatedUser = true;
87
+        if (this.connection && this.connection.connected &&
88
+            Strophe.getResourceFromJid(this.connection.jid)) {
89
+            // .connected is true while connecting?
90
+//                this.connection.send($pres());
91
+            this.eventEmitter.emit(
92
+                    JitsiConnectionEvents.CONNECTION_ESTABLISHED,
93
+                    Strophe.getResourceFromJid(this.connection.jid));
94
+        }
95
+    } else if (status === Strophe.Status.CONNFAIL) {
96
+        if (msg === 'x-strophe-bad-non-anon-jid') {
97
+            this.anonymousConnectionFailed = true;
98
+        } else {
99
+            this.connectionFailed = true;
100
+        }
101
+        this.lastErrorMsg = msg;
102
+    } else if (status === Strophe.Status.DISCONNECTED) {
103
+        // Stop ping interval
104
+        this.connection.ping.stopInterval();
105
+        this.disconnectInProgress = false;
106
+        if (this.anonymousConnectionFailed) {
107
+            // prompt user for username and password
108
+            this.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
109
+                JitsiConnectionErrors.PASSWORD_REQUIRED);
110
+        } else if(this.connectionFailed) {
111
+            this.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
112
+                JitsiConnectionErrors.OTHER_ERROR,
113
+                msg ? msg : this.lastErrorMsg);
114
+        } else {
115
+            this.eventEmitter.emit(
116
+                    JitsiConnectionEvents.CONNECTION_DISCONNECTED,
117
+                    msg ? msg : this.lastErrorMsg);
118
+        }
119
+    } else if (status === Strophe.Status.AUTHFAIL) {
120
+        // wrong password or username, prompt user
121
+        this.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
122
+            JitsiConnectionErrors.PASSWORD_REQUIRED);
123
+
124
+    }
125
+}
126
+
57
 XMPP.prototype._connect = function (jid, password) {
127
 XMPP.prototype._connect = function (jid, password) {
58
-    var self = this;
59
     // connection.connect() starts the connection process.
128
     // connection.connect() starts the connection process.
60
     //
129
     //
61
     // As the connection process proceeds, the user supplied callback will
130
     // As the connection process proceeds, the user supplied callback will
83
     //  Status.DISCONNECTING - The connection is currently being terminated
152
     //  Status.DISCONNECTING - The connection is currently being terminated
84
     //  Status.ATTACHED - The connection has been attached
153
     //  Status.ATTACHED - The connection has been attached
85
 
154
 
86
-    var anonymousConnectionFailed = false;
87
-    var connectionFailed = false;
88
-    var lastErrorMsg;
89
-    this.connection.connect(jid, password, function (status, msg) {
90
-        logger.log("(TIME) Strophe " + Strophe.getStatusString(status) +
91
-            (msg ? "[" + msg + "]" : "") + "\t:" + window.performance.now());
92
-        if (status === Strophe.Status.CONNECTED) {
93
-            if (self.options.useStunTurn) {
94
-                self.connection.jingle.getStunAndTurnCredentials();
95
-            }
96
-
97
-            logger.info("My Jabber ID: " + self.connection.jid);
98
-
99
-            // Schedule ping ?
100
-            var pingJid = self.connection.domain;
101
-            self.connection.ping.hasPingSupport(
102
-                pingJid,
103
-                function (hasPing) {
104
-                    if (hasPing)
105
-                        self.connection.ping.startInterval(pingJid);
106
-                    else
107
-                        logger.warn("Ping NOT supported by " + pingJid);
108
-                }
109
-            );
110
-
111
-            if (password)
112
-                authenticatedUser = true;
113
-            if (self.connection && self.connection.connected &&
114
-                Strophe.getResourceFromJid(self.connection.jid)) {
115
-                // .connected is true while connecting?
116
-//                self.connection.send($pres());
117
-                self.eventEmitter.emit(
118
-                        JitsiConnectionEvents.CONNECTION_ESTABLISHED,
119
-                        Strophe.getResourceFromJid(self.connection.jid));
120
-            }
121
-        } else if (status === Strophe.Status.CONNFAIL) {
122
-            if (msg === 'x-strophe-bad-non-anon-jid') {
123
-                anonymousConnectionFailed = true;
124
-            } else {
125
-                connectionFailed = true;
126
-            }
127
-            lastErrorMsg = msg;
128
-        } else if (status === Strophe.Status.DISCONNECTED) {
129
-            // Stop ping interval
130
-            self.connection.ping.stopInterval();
131
-            self.disconnectInProgress = false;
132
-            if (anonymousConnectionFailed) {
133
-                // prompt user for username and password
134
-                self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
135
-                    JitsiConnectionErrors.PASSWORD_REQUIRED);
136
-            } else if(connectionFailed) {
137
-                self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
138
-                    JitsiConnectionErrors.OTHER_ERROR,
139
-                    msg ? msg : lastErrorMsg);
140
-            } else {
141
-                self.eventEmitter.emit(
142
-                        JitsiConnectionEvents.CONNECTION_DISCONNECTED,
143
-                        msg ? msg : lastErrorMsg);
144
-            }
145
-        } else if (status === Strophe.Status.AUTHFAIL) {
146
-            // wrong password or username, prompt user
147
-            self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
148
-                JitsiConnectionErrors.PASSWORD_REQUIRED);
155
+    this.anonymousConnectionFailed = false;
156
+    this.connectionFailed = false;
157
+    this.lastErrorMsg;
158
+    this.connection.connect(jid, password,
159
+        this.connectionHandler.bind(this, password));
160
+}
149
 
161
 
150
-        }
151
-    });
162
+/**
163
+ * Attach to existing connection. Can be used for optimizations. For example:
164
+ * if the connection is created on the server we can attach to it and start
165
+ * using it.
166
+ *
167
+ * @param options {object} connecting options - rid, sid, jid and password.
168
+ */
169
+ XMPP.prototype.attach = function (options) {
170
+    logger.log("(TIME) Strophe attaching\t:" + window.performance.now(),options, options.jid);
171
+    this.connection.attach(options.jid, options.sid, parseInt(options.rid,10)+1,
172
+        this.connectionHandler.bind(this, options.password));
152
 }
173
 }
153
 
174
 
154
 XMPP.prototype.connect = function (jid, password) {
175
 XMPP.prototype.connect = function (jid, password) {

Loading…
Cancel
Save