Skip to content

Commit

Permalink
Simplify HttpServer_Websockets sample
Browse files Browse the repository at this point in the history
(Discovered memory leak in `WebsocketConnection::send` - SmingHub#2749)
  • Loading branch information
mikee47 committed Mar 27, 2024
1 parent 112bbdf commit 34436b8
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions samples/HttpServer_WebSockets/app/CUserData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,24 @@ void CUserData::addSession(WebsocketConnection& connection)

void CUserData::removeSession(WebsocketConnection& connection)
{
int i = activeWebSockets.indexOf(&connection);
if(i < 0) {
return;
if(activeWebSockets.removeElement(&connection)) {
connection.setUserData(nullptr);
Serial.println(F("Removed user session"));
}

activeWebSockets[i]->setUserData(nullptr);
activeWebSockets.remove(i);
Serial.println(F("Removed user session"));
}

void CUserData::printMessage(WebsocketConnection& connection, const String& msg)
{
unsigned i = 0;
for(; i < activeWebSockets.count(); i++) {
if(connection == *(activeWebSockets[i])) {
break;
}
}

if(i < activeWebSockets.count()) {
Serial << _F("Received msg on connection ") << i << ": " << msg;
int i = activeWebSockets.indexOf(&connection);
if(i >= 0) {
Serial << _F("Received msg on connection ") << i << ": " << msg << endl;
}
}

void CUserData::logOut()
{
for(auto skt : activeWebSockets) {
skt->setUserData(nullptr);
for(auto socket : activeWebSockets) {
socket->setUserData(nullptr);
}

activeWebSockets.removeAllElements();
Expand Down

0 comments on commit 34436b8

Please # to comment.