How can detect server disconnect from client ESP8266WebServer

I have this code and I want to know when the connection is disconnected

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>


#define led0 4                //D2


DynamicJsonBuffer jsonBuffer;

const char *ssid      = "ssid";
const char *password  = "password";

int sensorValue0 = 0;        
       
String sensor_values;

ESP8266WebServer server(80);

 WiFiClient client;
void handleSentVar() {
  
  if (server.hasArg("sensor_reading"))
  {
    sensor_values = server.arg("sensor_reading");
    Serial.print("**********************sensorValue:");
    Serial.println(sensor_values);
  }
  JsonObject& root = jsonBuffer.parseObject(sensor_values);

      sensorValue0          = root["sensor0_reading"].as<int>();



  Serial.print("sensorValue0:");
  Serial.println(sensorValue0);


  toggle_leds();

  server.send(200, "text/html", "Data received");
}


void setup() {
  Serial.begin(9600);
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();

  pinMode(led0, OUTPUT);


  
  //toggle_leds();                 //turn off all leds as all the sensor values are zero
  
  server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  server.begin();

  
}

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

void toggle_leds()
{
  if (sensorValue0 == 0){  digitalWrite(led0, LOW);
   Serial.println("++++++++++++++++off+++++++++++++");}


  if (sensorValue0 == 1){  digitalWrite(led0, HIGH);
  Serial.println("------------on-------------");}

}

How can I do it?

I have this code and I want to know when the connection is disconnected

Why do you think you need it? The client is completely handled by the ESP8266 library, you don't even see it in your code and it's rather seldom that a client disconnects while a request is not yet answered.

pylon:
Why do you think you need it? The client is completely handled by the ESP8266 library, you don't even see it in your code and it's rather seldom that a client disconnects while a request is not yet answered.

Hi,I want to write LOW on the pin

digitalWrite(led0, LOW);

when the connection is disconnected

Hi,I want to write LOW on the pin

digitalWrite(led0, LOW);

when the connection is disconnected

So write a LOW to that pin constantly as the client usually disconnects after every request. Why is that important for you? What information gain do you expect?

Interesting code, looks like the various libraries handle the connect/disconnect internally within them.

I have found that this code can print multiple devices connected to the server

Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());

But after client disconnect it doesn't return to zero

I have found that this code can print multiple devices connected to the server

No, that code shows how many clients are registered to the sofware access point. That about the same as the number of devices that use the ESP8266 as their router (to use a commonly know term for that functionality).

What do you want to achieve?