diff --git a/lib/connection.js b/lib/connection.js index 00d20b6..062a1bb 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -23,8 +23,10 @@ Babylon.Connection.prototype.write_cookie = function() { var jid = this.jid; var sid = this.connection.sid; var rid = this.connection.rid; - console.log("writing cookie with jid: " + jid + ", sid: " + sid + ", rid: " + rid); - $.cookie("babylon", [jid, sid, rid].join(","), { path: '/' }); + if(jid != "" && sid != "" && rid != ""){ + // console.log("writing cookie with jid: " + jid + ", sid: " + sid + ", rid: " + rid); + $.cookie("babylon", [jid, sid, rid].join(","), { path: '/' }); + } }; // read jid, sid, rid and expires from cookie @@ -56,6 +58,22 @@ Babylon.Connection.prototype.register_cookie_callback = function() { window.onbeforeunload = function(){that.write_cookie()}; }; +Babylon.Connection.prototype.unregister_cookie_callback = function() { + window.onbeforeunload = null; +}; + +Babylon.Connection.prototype.reconnect_or_destroy_session = function() { + console.log("________________________ dissconnect received ________________________"); + this.erase_cookie(); + if(Babylon.config.reconnect){ + console.log("________________________ reconnecting ________________________"); + this.connect(Babylon.config.jid, Babylon.config.password); + }else{ + console.log("________________________ destroying session ________________________"); + this.unregister_cookie_callback(); + } +}; + Babylon.Connection.prototype.cookie_callback = function() { this.write_cookie(); return true; @@ -76,6 +94,8 @@ Babylon.Connection.prototype.on_connect = function(status, err) { switch(status) { case Strophe.Status.ERROR: Babylon.log.debug("status: error, " + err); + this.erase_cookie(); + this.unregister_cookie_callback(); this.on_error(err); break; @@ -96,7 +116,7 @@ Babylon.Connection.prototype.on_connect = function(status, err) { case Strophe.Status.DISCONNECTED: Babylon.log.debug("status: disconnected"); - this.erase_cookie(); + this.reconnect_or_destroy_session(); this.handler.on_status_change("disconnected"); break; diff --git a/lib/runner.js b/lib/runner.js index f4d3bef..e5b6c7b 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -27,9 +27,11 @@ Babylon.Runner.prototype.connect = function(jid, password, reattach_check){ can_login = this.should_reattach() ? false : true; } + // TODO need to set the login credentials in one way all the time + Babylon.config.jid = jid; + Babylon.config.password = password; + if(can_login){ - Babylon.config.jid = jid; - Babylon.config.password = password; Babylon.log.debug("Connecting to: " + Babylon.config.host + " with jid: " + Babylon.config.jid); Babylon.Runner.connection.connect(Babylon.config.jid, Babylon.config.password); }