Implement initial load through websocket to cut one unnecessary HTTP GET that caused conn pool overflow. Increased reload / retry delays to reduce server spamming by poorly loaded client.

http-comm
Ondřej Hruška 7 years ago
parent a20ec8a49e
commit 1d6d6ee228
  1. 261
      html_orig/jssrc/term_conn.js
  2. 15
      user/cgi_sockets.c

@ -1,131 +1,134 @@
/** Handle connections */ /** Handle connections */
var Conn = (function() { var Conn = (function () {
var ws; var ws;
var heartbeatTout; var heartbeatTout;
var pingIv; var pingIv;
var xoff = false; var xoff = false;
var autoXoffTout; var autoXoffTout;
var reconTout;
function onOpen(evt) {
console.log("CONNECTED"); var pageShown = false;
}
function onOpen(evt) {
function onClose(evt) { console.log("CONNECTED");
console.warn("SOCKET CLOSED, code "+evt.code+". Reconnecting..."); doSend("i");
setTimeout(function() { }
init();
}, 200); function onClose(evt) {
// this happens when the buffer gets fucked up via invalid unicode. console.warn("SOCKET CLOSED, code " + evt.code + ". Reconnecting...");
// we basically use polling instead of socket then clearTimeout(reconTout);
} reconTout = setTimeout(function () {
init();
function onMessage(evt) { }, 2000);
try { // this happens when the buffer gets fucked up via invalid unicode.
// . = heartbeat // we basically use polling instead of socket then
switch (evt.data.charAt(0)) { }
case 'B':
case 'T': function onMessage(evt) {
case 'S': try {
Screen.load(evt.data); // . = heartbeat
break; switch (evt.data.charAt(0)) {
case 'B':
case '-': case 'T':
//console.log('xoff'); case 'S':
xoff = true; Screen.load(evt.data);
autoXoffTout = setTimeout(function(){xoff=false;}, 250); if(!pageShown) {
break; showPage();
pageShown = true;
case '+': }
//console.log('xon'); break;
xoff = false;
clearTimeout(autoXoffTout); case '-':
break; //console.log('xoff');
} xoff = true;
heartbeat(); autoXoffTout = setTimeout(function () {
} catch(e) { xoff = false;
console.error(e); }, 250);
} break;
}
case '+':
function canSend() { //console.log('xon');
return !xoff; xoff = false;
} clearTimeout(autoXoffTout);
break;
function doSend(message) { }
if (_demo) { heartbeat();
console.log("TX: ", message); } catch (e) {
return true; // Simulate success console.error(e);
} }
if (xoff) { }
// TODO queue
console.log("Can't send, flood control."); function canSend() {
return false; return !xoff;
} }
if (!ws) return false; // for dry testing function doSend(message) {
if (ws.readyState != 1) { if (_demo) {
console.error("Socket not ready"); console.log("TX: ", message);
return false; return true; // Simulate success
} }
if (typeof message != "string") { if (xoff) {
message = JSON.stringify(message); // TODO queue
} console.log("Can't send, flood control.");
ws.send(message); return false;
return true; }
}
if (!ws) return false; // for dry testing
function init() { if (ws.readyState != 1) {
if (_demo) { console.error("Socket not ready");
console.log("Demo mode!"); return false;
Screen.load(_demo_screen); }
showPage(); if (typeof message != "string") {
return; message = JSON.stringify(message);
} }
heartbeat(); ws.send(message);
return true;
ws = new WebSocket("ws://"+_root+"/term/update.ws"); }
ws.onopen = onOpen;
ws.onclose = onClose; function init() {
ws.onmessage = onMessage; if (_demo) {
console.log("Demo mode!");
console.log("Opening socket."); Screen.load(_demo_screen);
showPage();
// Ask for initial data return;
$.get('http://'+_root+'/term/init', function(resp, status) { }
if (status !== 200) location.reload(true);
console.log("Data received!"); clearTimeout(reconTout);
Screen.load(resp); clearTimeout(heartbeatTout);
heartbeat();
ws = new WebSocket("ws://" + _root + "/term/update.ws");
showPage(); ws.onopen = onOpen;
}); ws.onclose = onClose;
} ws.onmessage = onMessage;
console.log("Opening socket.");
function heartbeat() { heartbeat();
clearTimeout(heartbeatTout); }
heartbeatTout = setTimeout(heartbeatFail, 2000);
} function heartbeat() {
clearTimeout(heartbeatTout);
function heartbeatFail() { heartbeatTout = setTimeout(heartbeatFail, 2000);
console.error("Heartbeat lost, probing server..."); }
pingIv = setInterval(function() {
console.log("> ping"); function heartbeatFail() {
$.get('http://'+_root+'/system/ping', function(resp, status) { console.error("Heartbeat lost, probing server...");
if (status == 200) { pingIv = setInterval(function () {
clearInterval(pingIv); console.log("> ping");
console.info("Server ready, reloading page..."); $.get('http://' + _root + '/system/ping', function (resp, status) {
location.reload(); if (status == 200) {
} clearInterval(pingIv);
}, { console.info("Server ready, reloading page...");
timeout: 100, location.reload();
}); }
}, 500); }, {
} timeout: 100,
});
return { }, 1000);
ws: null, }
init: init,
send: doSend, return {
canSend: canSend, // check flood control ws: null,
}; init: init,
send: doSend,
canSend: canSend, // check flood control
};
})(); })();

@ -47,6 +47,7 @@ notifyCooldownTimCb(void *arg)
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
notifyContentTimCb(void *arg) notifyContentTimCb(void *arg)
{ {
Websock *ws = arg;
void *data = NULL; void *data = NULL;
int max_bl, total_bl; int max_bl, total_bl;
char sock_buff[SOCK_BUF_LEN]; char sock_buff[SOCK_BUF_LEN];
@ -65,8 +66,14 @@ notifyContentTimCb(void *arg)
int flg = 0; int flg = 0;
if (cont == HTTPD_CGI_MORE) flg |= WEBSOCK_FLAG_MORE; if (cont == HTTPD_CGI_MORE) flg |= WEBSOCK_FLAG_MORE;
if (i > 0) flg |= WEBSOCK_FLAG_CONT; if (i > 0) flg |= WEBSOCK_FLAG_CONT;
cgiWebsockBroadcast(URL_WS_UPDATE, sock_buff, (int) strlen(sock_buff), flg); if (ws) {
cgiWebsocketSend(ws, sock_buff, (int) strlen(sock_buff), flg);
} else {
cgiWebsockBroadcast(URL_WS_UPDATE, sock_buff, (int) strlen(sock_buff), flg);
}
if (cont == HTTPD_CGI_DONE) break; if (cont == HTTPD_CGI_DONE) break;
system_soft_wdt_feed();
} }
// cleanup // cleanup
@ -251,6 +258,12 @@ void ICACHE_FLASH_ATTR updateSockRx(Websock *ws, char *data, int len, int flags)
} }
break; break;
case 'i':
// requests initial load
dbg("Client requests initial load");
notifyContentTimCb(ws);
break;
case 'm': case 'm':
case 'p': case 'p':
case 'r': case 'r':

Loading…
Cancel
Save