Skip to content

Commit

Permalink
- Fix memory leak by calling delete _server in the WebSocketsServer d…
Browse files Browse the repository at this point in the history
…estructor.

- Improve performance by looking for match to existing socket before creating a new one
  • Loading branch information
BetterComputing authored and Links2004 committed Jan 5, 2024
1 parent 241c73a commit 3352c83
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/WebSocketsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ WebSocketsServerCore::~WebSocketsServerCore() {
}

WebSocketsServer::~WebSocketsServer() {
delete _server;
}

/**
Expand Down Expand Up @@ -428,8 +429,16 @@ WSclient_t * WebSocketsServerCore::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclien
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i];

// look for match to existing socket before creating a new one
if (clientIsConnected(client))
{
// Check to see if it is the same socket - if so, return it
if (client->tcp->getSocketNumber() == TCPclient->getSocketNumber())
{
return client;
}
} else {
// state is not connected or tcp connection is lost
if(!clientIsConnected(client)) {
client->tcp = TCPclient;

#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
Expand Down

0 comments on commit 3352c83

Please sign in to comment.