Prevent memory exceptions using WS.

This commit is contained in:
Blaz Kristan 2021-12-18 10:25:58 +01:00
parent 9c84f13425
commit 37dbf4d8ec
2 changed files with 11 additions and 6 deletions

View File

@ -302,9 +302,9 @@
#endif #endif
#ifdef WLED_USE_DYNAMIC_JSON #ifdef WLED_USE_DYNAMIC_JSON
#define MIN_HEAP_SIZE JSON_BUFFER_SIZE+512 #define MIN_HEAP_SIZE (JSON_BUFFER_SIZE+512)
#else #else
#define MIN_HEAP_SIZE 4096 #define MIN_HEAP_SIZE (MAX_LED_MEMORY+2048)
#endif #endif
// Maximum size of node map (list of other WLED instances) // Maximum size of node map (list of other WLED instances)

View File

@ -117,8 +117,10 @@ void sendDataWs(AsyncWebSocketClient * client)
serializeInfo(info); serializeInfo(info);
DEBUG_PRINTF("JSON buffer size: %u for WS request.\n", doc.memoryUsage()); DEBUG_PRINTF("JSON buffer size: %u for WS request.\n", doc.memoryUsage());
size_t len = measureJson(doc); size_t len = measureJson(doc);
buffer = ws.makeBuffer(len); size_t heap1 = ESP.getFreeHeap();
if (!buffer) { buffer = ws.makeBuffer(len); // will not allocate correct memory sometimes
size_t heap2 = ESP.getFreeHeap();
if (!buffer || heap1-heap2<len) {
releaseJSONBufferLock(); releaseJSONBufferLock();
return; //out of memory return; //out of memory
} }
@ -139,10 +141,13 @@ void handleWs()
{ {
if (millis() - wsLastLiveTime > WS_LIVE_INTERVAL) if (millis() - wsLastLiveTime > WS_LIVE_INTERVAL)
{ {
#ifdef ESP8266
ws.cleanupClients(2);
#else
ws.cleanupClients(); ws.cleanupClients();
#endif
bool success = true; bool success = true;
if (wsLiveClientId) if (wsLiveClientId) success = serveLiveLeds(nullptr, wsLiveClientId);
success = serveLiveLeds(nullptr, wsLiveClientId);
wsLastLiveTime = millis(); wsLastLiveTime = millis();
if (!success) wsLastLiveTime -= 20; //try again in 20ms if failed due to non-empty WS queue if (!success) wsLastLiveTime -= 20; //try again in 20ms if failed due to non-empty WS queue
} }