Skip to content

Commit

Permalink
feature: added support for reconnecting automatically when disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
kernow committed Nov 25, 2009
1 parent 4d51a39 commit 4f8a8fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
26 changes: 23 additions & 3 deletions lib/connection.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions lib/runner.js
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4f8a8fa

Please sign in to comment.