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.
This commit is contained in:
JC Brand 2015-06-05 14:46:52 +02:00
parent 085d91b546
commit f86ef934e8

View File

@ -5671,52 +5671,45 @@
});
};
this.initConnection = function () {
var rid, sid, jid;
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();
this.attemptPreboundSession = function (tokens) {
/* Handle session resumption or initialization when prebind is being used.
*/
var rid = tokens.rid, jid = tokens.jid, sid = tokens.sid;
if (this.keepalive) {
if (!this.jid) {
throw new Error("initConnection: when using 'keepalive' with 'prebind, you must supply the JID of the current user.");
}
rid = this.session.get('rid');
sid = this.session.get('sid');
jid = this.session.get('jid');
if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) {
this.session.save({rid: rid}); // The RID needs to be increased with each request.
return this.connection.attach(jid, sid, rid, this.onConnStatusChanged);
}
if (this.authentication === PREBIND) {
if (!this.keepalive) {
} else { // Not keepalive
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 {
throw new Error("initConnection: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values");
}
}
if (rid && sid && jid && Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(this.jid)) {
this.session.save({rid: rid}); // The RID needs to be increased with each request.
this.connection.attach(jid, sid, rid, this.onConnStatusChanged);
} else if (this.prebind_url) {
// We haven't been able to attach yet. Let's see if there
// is a prebind_url, otherwise there's nothing with which
// we can attach.
if (this.prebind_url) {
this.startNewBOSHSession();
} else {
delete this.connection;
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) {
this.session.save({rid: rid}); // The RID needs to be increased with each request.
this.connection.attach(jid, sid, rid, this.onConnStatusChanged);
@ -5734,6 +5727,36 @@
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);
}
}
};