How to check if there is a (web) client connected. #124
-
Hi, After several evenings of googling and reading, I'm about to give up and implement a very dirty solution in order to check if there is a client connected to the web-server I am running. I build my own little ESP32-C3 board with an OLED display and I just want to display if there is a client connected or not. I have looked at the examples and digged through a large part of the lib trying to understand how things work. I also tried a few things, but none of them worked. Is there a way, using AsyncWebServer to check if a client is connected? Is there a status or client count I can access, or a callback I can use? I can see the information itself in the lib, but I can't find a neat way to access it. Cheers, Henk |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You need to define what this means for you.
Yes, but depending on what you mean and/or can be done easily (i.e. with a middleware) You just need to precise your intention, use case, meanings and we can help you ;-) |
Beta Was this translation helpful? Give feedback.
-
Here is for example a middleware that you can use to count the number of clients at one point on a handler (or set of handler): class ClientMonitorMiddleware : public AsyncMiddleware {
public:
void run(AsyncWebServerRequest *request, ArMiddlewareNext next) override {
_count++;
request->onDisconnect([this]() {
_count--;
});
next();
}
size_t clientCount() const {
return _count;
}
private:
size_t _count;
};
static ClientMonitorMiddleware myDownload; // will be applied to one handler
static ClientMonitorMiddleware allAPI; // will be applied to several ones that you can use like this: server.on("/api/foo", HTTP_GET, [](AsyncWebServerRequest *request) {
// [...]
}).addMiddleware(&allAPI);
server.on("/api/bar", HTTP_GET, [](AsyncWebServerRequest *request) {
// [...]
}).addMiddleware(&allAPI);
server.on("/download", HTTP_GET, [](AsyncWebServerRequest *request) {
// [...]
}).addMiddleware(& myDownload);
|
Beta Was this translation helpful? Give feedback.
Here is for example a middleware that you can use to count the number of clients at one point on a handler (or set of handler):
that you can use like this:
server.on("/api/foo", HTTP_GET, [](AsyncWebServerRequest *request) { …