Implemented heartbeat

pull/111/merge
Ondřej Hruška 7 years ago
parent f8ebae044a
commit 4edaa0687d
  1. 18
      html_orig/js/app.js
  2. 18
      html_orig/jssrc/term.js
  3. 10
      user/cgi_sockets.c

@ -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 {
// . = 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,

@ -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 {
// . = 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,

@ -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);
}

Loading…
Cancel
Save