WebServer.h how do i know if a client disconnects?

PepeTheGreat
Posts: 14
Joined: Mon Sep 18, 2023 3:54 am

WebServer.h how do i know if a client disconnects?

Postby PepeTheGreat » Sun Mar 17, 2024 8:32 am

My video streaming app does use server.sendContent(); to stream mjpeg pictures. This is done in a loop inside the webserver handler. This loop continues to send even if the client disconnects. How do i prevent this?

Using "WiFiServer" there is client.available() and client.connected() to check this.

Using "esp_http_server.h" the httpd_resp_send_chunk function gives the return ESP_OK on sucess.

How do i replicate this functionality with "WebServer.h"?
Or can i make the OTA updater functional with "WiFiServer" or "esp_http_server.h" ?

lbernstone
Posts: 673
Joined: Mon Jul 22, 2019 3:20 pm

Re: WebServer.h how do i know if a client disconnects?

Postby lbernstone » Sun Mar 17, 2024 7:25 pm

I am not so familiar with the code to have a quick answer for you, but the WiFiClient that WebServer is using is public, so in theory you should be able to look at that object to see if it is connected.
As far as an IDF-based wifi manager, esp-wifi-manager can be included in a project as a component.

PepeTheGreat
Posts: 14
Joined: Mon Sep 18, 2023 3:54 am

Re: WebServer.h how do i know if a client disconnects?

Postby PepeTheGreat » Mon Mar 18, 2024 1:52 pm

lbernstone wrote:
Sun Mar 17, 2024 7:25 pm
the WiFiClient that WebServer is using is public, so in theory you should be able to look at that object to see if it is connected.

TY. That might exceed my talent. Will try.

Project:
http video:
https://youtu.be/NCWrQaDqzJg
external video:
https://youtube.com/shorts/Xo_1b1SBKy8

lbernstone
Posts: 673
Joined: Mon Jul 22, 2019 3:20 pm

Re: WebServer.h how do i know if a client disconnects?

Postby lbernstone » Mon Mar 18, 2024 7:39 pm

Code: Select all

#include <WebServer.h>

WebServer server(80);

void handleRoot() {
  WiFiClient wfc = server.client();
  if (wfc.connected()) {
    Serial.print("Remote IP: ");
    Serial.println(wfc.remoteIP());
  }
  server.send(200, "text/plain", "Hello!\n");
}

void setup() {
  Serial.begin(115200);
  WiFi.begin("larryb","clownfish");
  WiFi.waitForConnectResult();
  Serial.println(WiFi.localIP());
  server.on("/", HTTP_ANY, [](){handleRoot();});
  server.begin();
}

void loop() {
  server.handleClient();
  delay(100);
}

PepeTheGreat
Posts: 14
Joined: Mon Sep 18, 2023 3:54 am

Re: WebServer.h how do i know if a client disconnects?

Postby PepeTheGreat » Mon Mar 18, 2024 9:11 pm

👍 👍 👍

Code: Select all

  while(wfc.connected())
  {
Works perfectly. TY

Next thing is mounting esp32cam on this:
Attachments
IMG_20240312_061210.jpg
IMG_20240312_061210.jpg (486.33 KiB) Viewed 481 times

Who is online

Users browsing this forum: No registered users and 123 guests