improved disconnection handler

This commit is contained in:
Rodolphe Breard 2012-11-13 16:29:52 +01:00
parent a053c8a99d
commit 3497037981

View file

@ -62,8 +62,6 @@ NsClient.prototype.connect = function() {
if (!this.is_connected) { if (!this.is_connected) {
this.storage.get(null, cnt(this)); this.storage.get(null, cnt(this));
} else {
console.warn("already connected");
} }
}; };
@ -72,8 +70,7 @@ NsClient.prototype.disconnect = function() {
chrome.socket.disconnect(this.socket); chrome.socket.disconnect(this.socket);
this.is_connected = false; this.is_connected = false;
console.info("disconnected"); console.info("disconnected");
} else { this.updateStatus();
console.warn("not connected");
} }
}; };
@ -85,23 +82,31 @@ NsClient.prototype.daemonize = function() {
if (data.substr(0, 5) === "ping ") { if (data.substr(0, 5) === "ping ") {
chrome.socket.write(elem.socket, rd_inf.data, function(w_inf) { chrome.socket.write(elem.socket, rd_inf.data, function(w_inf) {
chrome.socket.read(elem.socket, null, this); elem.daemonize();
}); });
} else {
elem.daemonize();
} }
} else {
/*
* A read error is the only way to know if the remote peer has disconnected.
* See <http://developer.chrome.com/apps/socket.html> for more informations.
*/
elem.disconnect();
console.info('connection lost, reconnecting...');
} }
}; };
}; };
if (this.is_connected) { if (this.is_connected) {
chrome.socket.read(this.socket, null, dm(this)); chrome.socket.read(this.socket, dm(this));
} else { } else {
console.warn("not connected"); console.error("unable to daemonize: not connected");
} }
}; };
NsClient.prototype.updateStatus = function() { NsClient.prototype.updateStatus = function() {
var status_txt = document.getElementById("status_txt"); var status_txt = document.getElementById("status_txt");
console.log(status_txt);
if (this.is_connected) { if (this.is_connected) {
if (status_txt !== null) if (status_txt !== null)