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. 45
      html_orig/jssrc/term_conn.js
  2. 13
      user/cgi_sockets.c

@ -1,20 +1,25 @@
/** Handle connections */
var Conn = (function() {
var Conn = (function () {
var ws;
var heartbeatTout;
var pingIv;
var xoff = false;
var autoXoffTout;
var reconTout;
var pageShown = false;
function onOpen(evt) {
console.log("CONNECTED");
doSend("i");
}
function onClose(evt) {
console.warn("SOCKET CLOSED, code "+evt.code+". Reconnecting...");
setTimeout(function() {
console.warn("SOCKET CLOSED, code " + evt.code + ". Reconnecting...");
clearTimeout(reconTout);
reconTout = setTimeout(function () {
init();
}, 200);
}, 2000);
// this happens when the buffer gets fucked up via invalid unicode.
// we basically use polling instead of socket then
}
@ -27,12 +32,18 @@ var Conn = (function() {
case 'T':
case 'S':
Screen.load(evt.data);
if(!pageShown) {
showPage();
pageShown = true;
}
break;
case '-':
//console.log('xoff');
xoff = true;
autoXoffTout = setTimeout(function(){xoff=false;}, 250);
autoXoffTout = setTimeout(function () {
xoff = false;
}, 250);
break;
case '+':
@ -42,7 +53,7 @@ var Conn = (function() {
break;
}
heartbeat();
} catch(e) {
} catch (e) {
console.error(e);
}
}
@ -81,24 +92,16 @@ var Conn = (function() {
showPage();
return;
}
heartbeat();
ws = new WebSocket("ws://"+_root+"/term/update.ws");
clearTimeout(reconTout);
clearTimeout(heartbeatTout);
ws = new WebSocket("ws://" + _root + "/term/update.ws");
ws.onopen = onOpen;
ws.onclose = onClose;
ws.onmessage = onMessage;
console.log("Opening socket.");
// Ask for initial data
$.get('http://'+_root+'/term/init', function(resp, status) {
if (status !== 200) location.reload(true);
console.log("Data received!");
Screen.load(resp);
heartbeat();
showPage();
});
}
function heartbeat() {
@ -108,9 +111,9 @@ var Conn = (function() {
function heartbeatFail() {
console.error("Heartbeat lost, probing server...");
pingIv = setInterval(function() {
pingIv = setInterval(function () {
console.log("> ping");
$.get('http://'+_root+'/system/ping', function(resp, status) {
$.get('http://' + _root + '/system/ping', function (resp, status) {
if (status == 200) {
clearInterval(pingIv);
console.info("Server ready, reloading page...");
@ -119,7 +122,7 @@ var Conn = (function() {
}, {
timeout: 100,
});
}, 500);
}, 1000);
}
return {

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

Loading…
Cancel
Save