Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4. Improve memory usage related to websocket #182

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/ESPDash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,21 @@ void ESPDash::remove(Statistic *statistic) {

// generates the layout JSON string to the frontend
size_t ESPDash::generateLayoutJSON(AsyncWebSocketClient *client, bool changes_only) {
// https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
// Browsers sometimes do not correctly close the websocket connection, even when the close() function is called in javascript.
// This will eventually exhaust the web server's resources and will cause the server to crash.
// Use DEFAULT_MAX_WS_CLIENTS or DASH_MAX_WS_CLIENTS (specific to ESP-DASH) to set the maximum number of clients
_ws->cleanupClients();

const size_t clients = _ws->count();

if (clients == 0) {
// do not consume cpu and memory if no client is connected
return 0;
}

String buf = "";
buf.reserve(DASH_LAYOUT_JSON_SIZE);
buf.reserve(changes_only ? DASH_PARTIAL_UPDATE_JSON_SIZE : DASH_LAYOUT_JSON_SIZE);

if (changes_only) {
buf += "{\"command\":\"update:components\",";
Expand Down Expand Up @@ -315,6 +328,9 @@ size_t ESPDash::generateLayoutJSON(AsyncWebSocketClient *client, bool changes_on
// Store the length of the JSON string
size_t total = buf.length();
// Send resp
#ifdef DASH_DEBUG
Serial.printf("client=%d, count=%d, changes_only=%d, total=%d\n%s\n", (client == nullptr ? -1 : client->id()), clients, changes_only, total, buf.c_str());
#endif
if (client != nullptr) {
_ws->text(client->id(), buf.c_str(), total);
} else {
Expand Down Expand Up @@ -461,6 +477,9 @@ void ESPDash::refreshStatistics() {
generateLayoutJSON(nullptr, true);
}

bool ESPDash::hasClient() {
return _ws->count() > 0;
}

/*
Destructor
Expand Down
10 changes: 8 additions & 2 deletions src/ESPDash.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Github URL: https://github.com/ayushsharma82/ESP-DASH
#define DASH_LAYOUT_JSON_SIZE 1024 * 5
#endif

#ifndef DASH_STATISTICS_JSON_SIZE
#define DASH_STATISTICS_JSON_SIZE 1024 * 1
#ifndef DASH_PARTIAL_UPDATE_JSON_SIZE
#define DASH_PARTIAL_UPDATE_JSON_SIZE DASH_LAYOUT_JSON_SIZE
#endif

#ifndef DASH_CARD_JSON_SIZE
Expand All @@ -60,6 +60,10 @@ Github URL: https://github.com/ayushsharma82/ESP-DASH
#define DASH_USE_LEGACY_CHART_STORAGE 0
#endif

#ifndef DASH_MAX_WS_CLIENTS
#define DASH_MAX_WS_CLIENTS DEFAULT_MAX_WS_CLIENTS
#endif

// Forward Declaration
class Card;
class Chart;
Expand Down Expand Up @@ -118,6 +122,8 @@ class ESPDash{

void refreshStatistics();

bool hasClient();

~ESPDash();
};

Expand Down
Loading