Slight websocket reconnection tweaks

This commit is contained in:
cschwinne 2022-02-19 22:47:17 +01:00
parent 7f92607b85
commit efbb7a034c
3 changed files with 826 additions and 821 deletions

View File

@ -215,7 +215,7 @@ function onLoad() {
//TODO: do some parsing first
})
.catch(function (error) {
console.log("holidays.json does not contain array of holidays. Defaults loaded.");
console.log("No array of holidays in holidays.json. Defaults loaded.");
})
.finally(function(){
loadBg(cfg.theme.bg.url);
@ -280,12 +280,8 @@ function showToast(text, error = false) {
}
function showErrorToast() {
if (ws && ws.readyState === WebSocket.OPEN) {
// if we received a timeout force WS reconnect
ws.close();
ws = null;
if (lastinfo.ws > -1) setTimeout(makeWS,500);
}
reconnectWS();
showToast('Connection to light failed!', true);
}
function clearErrorToast() {
@ -959,6 +955,13 @@ function cmpP(a, b) {
return a[1].n.localeCompare(b[1].n,undefined, {numeric: true});
}
//forces a WebSockets reconnect if timeout (error toast), or successful HTTP response to JSON request
function reconnectWS() {
if (ws) ws.close();
ws = null;
if (lastinfo && lastinfo.ws > -1) setTimeout(makeWS,500);
}
function makeWS() {
if (ws) return;
ws = new WebSocket('ws://'+(loc?locip:window.location.hostname)+'/ws');
@ -981,12 +984,13 @@ function makeWS() {
readState(json.state);
};
ws.onclose = (e)=>{
//if there is already a new web socket open, do not null ws
if (ws && ws.readyState === WebSocket.OPEN) return;
d.getElementById('connind').style.backgroundColor = "#831";
ws = null;
if (lastinfo.ws > -1) setTimeout(makeWS,500); //retry WS connection
}
ws.onopen = (e)=>{
ws.send("{'v':true}");
reqsLegal = true;
}
}
@ -1140,6 +1144,7 @@ function requestJson(command, rinfo = true) {
return;
}
var s = json;
if (reqsLegal && !ws) reconnectWS();
if (!command || rinfo) { //we have info object
if (!rinfo) { //entire JSON (on load)
@ -1464,8 +1469,8 @@ function makePlEntry(p,i) {
}
function makePlUtil() {
if (pNum < 2) {
showToast("You need at least 2 presets to make a playlist!"); return;
if (pNum < 1) {
showToast("Please make a preset first!"); return;
}
if (plJson[0].transition[0] < 0) plJson[0].transition[0] = tr;
d.getElementById('putil').innerHTML = `<div class="seg pres">

File diff suppressed because it is too large Load Diff

View File

@ -113,6 +113,7 @@ void sendDataWs(AsyncWebSocketClient * client)
size_t heap2 = ESP.getFreeHeap();
if (!buffer || heap1-heap2<len) {
releaseJSONBufferLock();
ws.closeAll(1013); //code 1013 = temporary overload, try again later
ws.cleanupClients(0); //disconnect all clients to release memory
return; //out of memory
}
@ -159,7 +160,7 @@ void handleWs()
if (millis() - wsLastLiveTime > WS_LIVE_INTERVAL)
{
#ifdef ESP8266
ws.cleanupClients(2);
ws.cleanupClients(3);
#else
ws.cleanupClients();
#endif