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

Add function so that the total number of web-socket clients can be limited #591

Merged
merged 23 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bf17874
Add function so that the total number of web-socket clients can be li…
matt123p Aug 24, 2019
fc6b237
Add thread locking to improve stability.
matt123p Aug 25, 2019
e6caec1
Prevent an assertion failure when using WebSockets
matt123p Aug 29, 2019
9777213
Add thread locking to improve stability.
matt123p Aug 25, 2019
62aff2d
Merge remote-tracking branch 'upstream/master'
matt123p Sep 8, 2019
1a285ce
Do not use thread locking with the ESP8266.
matt123p Sep 9, 2019
cfb07e0
Do not use thread locking with the ESP8266.
matt123p Sep 9, 2019
d811a12
Do not use thread locking with the ESP8266, but instead use an empty …
matt123p Sep 9, 2019
b47bc10
Do not use thread locking with the ESP8266, but instead use an empty …
matt123p Sep 9, 2019
d75c344
Add function so that the total number of web-socket clients can be li…
matt123p Aug 24, 2019
9768e84
Fix whitespace error.
matt123p Sep 10, 2019
45971a8
Merge branch 'websockets-thread-safety'
matt123p Sep 10, 2019
2a2655f
Fix whitespace error.
matt123p Sep 10, 2019
8de6205
Merge remote-tracking branch 'upstream/master'
matt123p Sep 22, 2019
7673a08
Merge branch 'master' into websockets-limit-clients
matt123p Sep 22, 2019
0e93acf
Update examples and documentation.
matt123p Sep 22, 2019
603fa07
Add new entry to contents.
matt123p Sep 22, 2019
e976927
Fix whitespace error.
matt123p Sep 22, 2019
97a49c2
Fix build.
matt123p Sep 22, 2019
d3da023
Merge branch 'master' into websockets-limit-clients
me-no-dev Sep 24, 2019
80ca420
Set the default number of ws clients dependent on processor.
matt123p Sep 24, 2019
c2d3290
Merge branch 'master' into websockets-limit-clients
me-no-dev Sep 24, 2019
722b4b3
Merge branch 'master' into websockets-limit-clients
me-no-dev Sep 24, 2019
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ To use this library you might need to have the latest git versions of [ESP32](ht
- [Async WebSocket Event](#async-websocket-event)
- [Methods for sending data to a socket client](#methods-for-sending-data-to-a-socket-client)
- [Direct access to web socket message buffer](#direct-access-to-web-socket-message-buffer)
- [Limiting the number of web socket clients](#limiting-the-number-of-web-socket-clients)
- [Async Event Source Plugin](#async-event-source-plugin)
- [Setup Event Source on the server](#setup-event-source-on-the-server)
- [Setup Event Source in the browser](#setup-event-source-in-the-browser)
Expand Down Expand Up @@ -1126,6 +1127,16 @@ void sendDataWs(AsyncWebSocketClient * client)
}
```

### 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. Periodically calling the cleanClients() function from the main loop() function limits the number of clients by closing the oldest client when the maximum number of clients has been exceeded. This can called be every cycle, however, if you wish to use less power, then calling as infrequently as once per second is sufficient.

```cpp
void loop(){
ws.cleanupClients();
}
```


## Async Event Source Plugin
The server includes EventSource (Server-Sent Events) plugin which can be used to send short text events to the browser.
Difference between EventSource and WebSockets is that EventSource is single direction, text-only protocol.
Expand Down
1 change: 1 addition & 0 deletions examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,5 @@ void setup(){

void loop(){
ArduinoOTA.handle();
ws.cleanupClients();
}
7 changes: 7 additions & 0 deletions src/AsyncWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,13 @@ void AsyncWebSocket::closeAll(uint16_t code, const char * message){
}
}

void AsyncWebSocket::cleanupClients(uint16_t maxClients)
{
if (count() > maxClients){
_clients.front()->close();
}
}

void AsyncWebSocket::ping(uint32_t id, uint8_t *data, size_t len){
AsyncWebSocketClient * c = client(id);
if(c)
Expand Down
7 changes: 7 additions & 0 deletions src/AsyncWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#include <Hash.h>
#endif

#ifdef ESP32
#define DEFAULT_MAX_WS_CLIENTS 8
#else
#define DEFAULT_MAX_WS_CLIENTS 4
#endif

class AsyncWebSocket;
class AsyncWebSocketResponse;
class AsyncWebSocketClient;
Expand Down Expand Up @@ -255,6 +261,7 @@ class AsyncWebSocket: public AsyncWebHandler {

void close(uint32_t id, uint16_t code=0, const char * message=NULL);
void closeAll(uint16_t code=0, const char * message=NULL);
void cleanupClients(uint16_t maxClients = DEFAULT_MAX_WS_CLIENTS);

void ping(uint32_t id, uint8_t *data=NULL, size_t len=0);
void pingAll(uint8_t *data=NULL, size_t len=0); // done
Expand Down