Skip to content

How to check if there is a (web) client connected. #124

Answered by mathieucarbou
HenkHoldijk asked this question in Q&A
Discussion options

You must be logged in to vote

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) {
…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@HenkHoldijk
Comment options

Comment options

You must be logged in to vote
4 replies
@HenkHoldijk
Comment options

@mathieucarbou
Comment options

@HenkHoldijk
Comment options

@mathieucarbou
Comment options

Answer selected by HenkHoldijk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants