WS reconnect logic & WS memory leak protection

This commit is contained in:
Blaž Kristan 2022-02-17 12:45:50 +01:00
parent e9a05890a5
commit 3be4b69b44
3 changed files with 810 additions and 795 deletions

View File

@ -956,7 +956,7 @@ function cmpP(a, b) {
function makeWS() {
if (ws) return;
ws = new WebSocket('ws://'+(loc?locip:window.location.hostname)+'/ws');
ws.binaryType = "arraybuffer";
ws.binaryType = "arraybuffer";
ws.onmessage = function(event) {
if (event.data instanceof ArrayBuffer) return; //liveview packet
var json = JSON.parse(event.data);
@ -974,9 +974,15 @@ function makeWS() {
displayRover(info, s);
readState(json.state);
};
ws.onclose = function(event) {
d.getElementById('connind').style.backgroundColor = "#831";
}
ws.onclose = (e)=>{
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;
}
}
function readState(s,command=false) {

File diff suppressed because it is too large Load Diff

View File

@ -108,9 +108,12 @@ void sendDataWs(AsyncWebSocketClient * client)
JsonObject info = doc.createNestedObject("info");
serializeInfo(info);
size_t len = measureJson(doc);
buffer = ws.makeBuffer(len);
if (!buffer) {
size_t heap1 = ESP.getFreeHeap();
buffer = ws.makeBuffer(len); // will not allocate correct memory sometimes
size_t heap2 = ESP.getFreeHeap();
if (!buffer || heap1-heap2<len) {
releaseJSONBufferLock();
ws.cleanupClients(0); // disconnect all clients to release memory
return; //out of memory
}
serializeJson(doc, (char *)buffer->get(), len +1);
@ -155,7 +158,11 @@ void handleWs()
{
if (millis() - wsLastLiveTime > WS_LIVE_INTERVAL)
{
#ifdef ESP8266
ws.cleanupClients(2);
#else
ws.cleanupClients();
#endif
bool success = true;
if (wsLiveClientId)
success = sendLiveLedsWs(wsLiveClientId);