Commit f86ef934 authored by JC Brand's avatar JC Brand

Refactor the session resumption code and fix a bug after recent merge.

Split the code into two new methods for the prebind and non-prebind
usecase.
parent 085d91b5
...@@ -5671,52 +5671,45 @@ ...@@ -5671,52 +5671,45 @@
}); });
}; };
this.initConnection = function () { this.attemptPreboundSession = function (tokens) {
var rid, sid, jid; /* Handle session resumption or initialization when prebind is being used.
if (this.connection && this.connection.connected) { */
this.setUpXMLLogging(); var rid = tokens.rid, jid = tokens.jid, sid = tokens.sid;
this.onConnected();
} else {
if (!this.bosh_service_url && ! this.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this.connection = new Strophe.Connection(this.websocket_url);
} else if (this.bosh_service_url) {
this.connection = new Strophe.Connection(this.bosh_service_url);
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
this.setUpXMLLogging();
if (this.keepalive) { if (this.keepalive) {
if (!this.jid) { if (!this.jid) {
throw new Error("initConnection: when using 'keepalive' with 'prebind, you must supply the JID of the current user."); throw new Error("initConnection: when using 'keepalive' with 'prebind, you must supply the JID of the current user.");
} }
rid = this.session.get('rid'); if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) {
sid = this.session.get('sid'); this.session.save({rid: rid}); // The RID needs to be increased with each request.
jid = this.session.get('jid'); return this.connection.attach(jid, sid, rid, this.onConnStatusChanged);
} }
if (this.authentication === PREBIND) { } else { // Not keepalive
if (!this.keepalive) {
if (this.jid && this.sid && this.rid) { if (this.jid && this.sid && this.rid) {
this.connection.attach(this.jid, this.sid, this.rid, this.onConnStatusChanged); return this.connection.attach(this.jid, this.sid, this.rid, this.onConnStatusChanged);
} else { } else {
throw new Error("initConnection: If you use prebind and not keepalive, "+ throw new Error("initConnection: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values"); "then you MUST supply JID, RID and SID values");
} }
} }
if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) { // We haven't been able to attach yet. Let's see if there
this.session.save({rid: rid}); // The RID needs to be increased with each request. // is a prebind_url, otherwise there's nothing with which
this.connection.attach(jid, sid, rid, this.onConnStatusChanged); // we can attach.
} else if (this.prebind_url) { if (this.prebind_url) {
this.startNewBOSHSession(); this.startNewBOSHSession();
} else { } else {
delete this.connection; delete this.connection;
this.emit('noResumeableSession'); this.emit('noResumeableSession');
} }
} else { };
// Non-prebind case.
this.attemptNonPreboundSession = function (tokens) {
/* Handle session resumption or initialization when prebind is not being used.
*
* Two potential options exist and are handled in this method:
* 1. keepalive
* 2. auto_login
*/
var rid = tokens.rid, jid = tokens.jid, sid = tokens.sid;
if (this.keepalive && rid && sid && jid) { if (this.keepalive && rid && sid && jid) {
this.session.save({rid: rid}); // The RID needs to be increased with each request. this.session.save({rid: rid}); // The RID needs to be increased with each request.
this.connection.attach(jid, sid, rid, this.onConnStatusChanged); this.connection.attach(jid, sid, rid, this.onConnStatusChanged);
...@@ -5734,6 +5727,36 @@ ...@@ -5734,6 +5727,36 @@
this.connection.connect(this.jid, this.password, this.onConnStatusChanged); this.connection.connect(this.jid, this.password, this.onConnStatusChanged);
} }
} }
};
this.initConnection = function () {
var tokens = {};
if (this.connection && this.connection.connected) {
this.setUpXMLLogging();
this.onConnected();
} else {
if (!this.bosh_service_url && ! this.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this.connection = new Strophe.Connection(this.websocket_url);
} else if (this.bosh_service_url) {
this.connection = new Strophe.Connection(this.bosh_service_url);
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
this.setUpXMLLogging();
if (this.keepalive) {
tokens.rid = this.session.get('rid');
tokens.sid = this.session.get('sid');
tokens.jid = this.session.get('jid');
}
// We now try to resume or automatically set up a new session.
// Otherwise the user will be shown a login form.
if (this.authentication === PREBIND) {
this.attemptPreboundSession(tokens);
} else {
this.attemptNonPreboundSession(tokens);
} }
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment