From 34970379818ae83f61f7662547f089bf31d1aee3 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Tue, 13 Nov 2012 16:29:52 +0100 Subject: [PATCH] improved disconnection handler --- lib/ns_client.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/ns_client.js b/lib/ns_client.js index 40119cd..b4d4a2a 100644 --- a/lib/ns_client.js +++ b/lib/ns_client.js @@ -62,8 +62,6 @@ NsClient.prototype.connect = function() { if (!this.is_connected) { this.storage.get(null, cnt(this)); - } else { - console.warn("already connected"); } }; @@ -72,8 +70,7 @@ NsClient.prototype.disconnect = function() { chrome.socket.disconnect(this.socket); this.is_connected = false; console.info("disconnected"); - } else { - console.warn("not connected"); + this.updateStatus(); } }; @@ -85,23 +82,31 @@ NsClient.prototype.daemonize = function() { if (data.substr(0, 5) === "ping ") { 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 for more informations. + */ + elem.disconnect(); + console.info('connection lost, reconnecting...'); } }; }; if (this.is_connected) { - chrome.socket.read(this.socket, null, dm(this)); + chrome.socket.read(this.socket, dm(this)); } else { - console.warn("not connected"); + console.error("unable to daemonize: not connected"); } }; NsClient.prototype.updateStatus = function() { var status_txt = document.getElementById("status_txt"); - console.log(status_txt); if (this.is_connected) { if (status_txt !== null)