Commit 0c61a5d7 authored by JC Brand's avatar JC Brand

core: Refactor common code into `restoreBOSHSession`

parent 5dd3b93a
...@@ -30,7 +30,9 @@ ...@@ -30,7 +30,9 @@
_converse.keepalive = true; _converse.keepalive = true;
_converse.authentication = "prebind"; _converse.authentication = "prebind";
expect(_converse.logIn.bind(_converse)).toThrow( expect(_converse.logIn.bind(_converse)).toThrow(
new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, you must supply the JID of the current user.")); new Error(
"restoreBOSHSession: tried to restore a \"keepalive\" session "+
"but we don't have the JID for the user!"));
_converse.authentication= authentication; _converse.authentication= authentication;
_converse.jid = jid; _converse.jid = jid;
_converse.keepalive = false; _converse.keepalive = false;
......
...@@ -1894,29 +1894,46 @@ ...@@ -1894,29 +1894,46 @@
xhr.send(); xhr.send();
}; };
this.restoreBOSHSession = function () {
/* Tries to restore a cached BOSH session. */
if (!this.jid) {
throw new Error(
"restoreBOSHSession: tried to restore a \"keepalive\" session "+
"but we don't have the JID for the user!");
}
try {
this.connection.restore(this.jid, this.onConnectStatusChanged);
return true;
} catch (e) {
this.log(
"Could not restore session for jid: "+
this.jid+" Error message: "+e.message);
this.clearSession(); // If there's a roster, we want to clear it (see #555)
return false;
}
};
this.attemptPreboundSession = function (reconnecting) { this.attemptPreboundSession = function (reconnecting) {
/* Handle session resumption or initialization when prebind is being used. /* Handle session resumption or initialization when prebind is
* being used.
*/ */
if (!reconnecting && this.keepalive) { if (!reconnecting) {
if (!this.jid) { if (this.keepalive && this.restoreBOSHSession()) {
throw new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, "+ return;
"you must supply the JID of the current user.");
} }
try { // No keepalive, or session resumption has failed.
return this.connection.restore(this.jid, this.onConnectStatusChanged); if (this.jid && this.sid && this.rid) {
} catch (e) { return this.connection.attach(
this.log("Could not restore session for jid: "+this.jid+" Error message: "+e.message); this.jid, this.sid, this.rid,
this.clearSession(); // If there's a roster, we want to clear it (see #555) this.onConnectStatusChanged
);
} }
} }
if (this.prebind_url) {
// No keepalive, or session resumption has failed.
if (!reconnecting && this.jid && this.sid && this.rid) {
return this.connection.attach(this.jid, this.sid, this.rid, this.onConnectStatusChanged);
} else if (this.prebind_url) {
return this.startNewBOSHSession(); return this.startNewBOSHSession();
} else { } else {
throw new Error("attemptPreboundSession: If you use prebind and not keepalive, "+ throw new Error(
"attemptPreboundSession: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values or a prebind_url."); "then you MUST supply JID, RID and SID values or a prebind_url.");
} }
}; };
...@@ -1966,13 +1983,8 @@ ...@@ -1966,13 +1983,8 @@
* 1. keepalive * 1. keepalive
* 2. auto_login * 2. auto_login
*/ */
if (this.keepalive && !reconnecting) { if (!reconnecting && this.keepalive && this.restoreBOSHSession()) {
try { return;
return this.connection.restore(this.jid, this.onConnectStatusChanged);
} catch (e) {
this.log("Could not restore session. Error message: "+e.message);
this.clearSession(); // If there's a roster, we want to clear it (see #555)
}
} }
if (this.auto_login) { if (this.auto_login) {
if (credentials) { if (credentials) {
...@@ -1983,14 +1995,14 @@ ...@@ -1983,14 +1995,14 @@
this.fetchLoginCredentials().done(this.autoLogin.bind(this)); this.fetchLoginCredentials().done(this.autoLogin.bind(this));
} else if (!this.jid) { } else if (!this.jid) {
throw new Error( throw new Error(
"initConnection: If you use auto_login, you also need"+ "attemptNonPreboundSession: If you use auto_login, "+
"to give either a jid value (and if applicable a "+ "you also need to give either a jid value (and if "+
"password) or you need to pass in a URL from where the "+ "applicable a password) or you need to pass in a URL "+
"username and password can be fetched (via credentials_url)." "from where the username and password can be fetched "+
"(via credentials_url)."
); );
} else { } else {
// Probably ANONYMOUS login this.autoLogin(); // Probably ANONYMOUS login
this.autoLogin();
} }
} else if (reconnecting) { } else if (reconnecting) {
this.autoLogin(); this.autoLogin();
......
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