diff --git a/html_orig/js/app.js b/html_orig/js/app.js index d9adf7a..8b344cb 100644 --- a/html_orig/js/app.js +++ b/html_orig/js/app.js @@ -1910,6 +1910,7 @@ var Screen = (function () { /** Handle connections */ var Conn = (function() { var ws; + var heartbeatTout; function onOpen(evt) { console.log("CONNECTED"); @@ -1926,9 +1927,13 @@ var Conn = (function() { function onMessage(evt) { try { - //console.log("RX: ", evt.data); - // Assume all our messages are screen updates - Screen.load(evt.data); + // . = heartbeat + if (evt.data != '.') { + //console.log("RX: ", evt.data); + // Assume all our messages are screen updates + Screen.load(evt.data); + } + heartbeat(); } catch(e) { console.error(e); } @@ -1949,6 +1954,8 @@ var Conn = (function() { } function init() { + heartbeat(); + ws = new WebSocket("ws://"+_root+"/term/update.ws"); ws.onopen = onOpen; ws.onclose = onClose; @@ -1961,11 +1968,22 @@ var Conn = (function() { if (status !== 200) location.reload(true); console.log("Data received!"); Screen.load(resp); + heartbeat(); showPage(); }); } + function heartbeat() { + clearTimeout(heartbeatTout); + heartbeatTout = setTimeout(heartbeatFail, 3000); + } + + function heartbeatFail() { + console.error("Heartbeat lost, reloading..."); + location.reload(); + } + return { ws: null, init: init, diff --git a/html_orig/jssrc/term.js b/html_orig/jssrc/term.js index 02428fd..711010f 100644 --- a/html_orig/jssrc/term.js +++ b/html_orig/jssrc/term.js @@ -353,6 +353,7 @@ var Screen = (function () { /** Handle connections */ var Conn = (function() { var ws; + var heartbeatTout; function onOpen(evt) { console.log("CONNECTED"); @@ -369,9 +370,13 @@ var Conn = (function() { function onMessage(evt) { try { - //console.log("RX: ", evt.data); - // Assume all our messages are screen updates - Screen.load(evt.data); + // . = heartbeat + if (evt.data != '.') { + //console.log("RX: ", evt.data); + // Assume all our messages are screen updates + Screen.load(evt.data); + } + heartbeat(); } catch(e) { console.error(e); } @@ -392,6 +397,8 @@ var Conn = (function() { } function init() { + heartbeat(); + ws = new WebSocket("ws://"+_root+"/term/update.ws"); ws.onopen = onOpen; ws.onclose = onClose; @@ -404,11 +411,22 @@ var Conn = (function() { if (status !== 200) location.reload(true); console.log("Data received!"); Screen.load(resp); + heartbeat(); showPage(); }); } + function heartbeat() { + clearTimeout(heartbeatTout); + heartbeatTout = setTimeout(heartbeatFail, 3000); + } + + function heartbeatFail() { + console.error("Heartbeat lost, reloading..."); + location.reload(); + } + return { ws: null, init: init, diff --git a/user/cgi_sockets.c b/user/cgi_sockets.c index 889379c..c3a164a 100644 --- a/user/cgi_sockets.c +++ b/user/cgi_sockets.c @@ -19,6 +19,7 @@ volatile bool notify_cooldown = false; static ETSTimer notifyContentTim; static ETSTimer notifyLabelsTim; static ETSTimer notifyCooldownTim; +static ETSTimer heartbeatTim; // we're trying to do a kind of mutex here, without the actual primitives // this might glitch, very rarely. @@ -183,9 +184,18 @@ void ICACHE_FLASH_ATTR updateSockRx(Websock *ws, char *data, int len, int flags) } } +void ICACHE_FLASH_ATTR heartbeatTimCb(void *unused) +{ + if (notify_available) { + cgiWebsockBroadcast(URL_WS_UPDATE, ".", 1, 0); + } +} + /** Socket connected for updates */ void ICACHE_FLASH_ATTR updateSockConnect(Websock *ws) { ws_info("Socket connected to "URL_WS_UPDATE); ws->recvCb = updateSockRx; + + TIMER_START(&heartbeatTim, heartbeatTimCb, 1000, 1); }