File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: LGPL-3.0-or-later
2+ // Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
3+
4+ //
5+ // Server state example
6+ //
7+
8+ #include < Arduino.h>
9+ #ifdef ESP32
10+ #include < AsyncTCP.h>
11+ #include < WiFi.h>
12+ #elif defined(ESP8266)
13+ #include < ESP8266WiFi.h>
14+ #include < ESPAsyncTCP.h>
15+ #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
16+ #include < RPAsyncTCP.h>
17+ #include < WiFi.h>
18+ #endif
19+
20+ #include < ESPAsyncWebServer.h>
21+
22+ static AsyncWebServer server1 (80 );
23+ static AsyncWebServer server2 (80 );
24+
25+ void setup () {
26+ Serial.begin (115200 );
27+
28+ #ifndef CONFIG_IDF_TARGET_ESP32H2
29+ WiFi.mode (WIFI_AP);
30+ WiFi.softAP (" esp-captive" );
31+ #endif
32+
33+ // server state returns one of the tcp_state enum values:
34+ // enum tcp_state {
35+ // CLOSED = 0,
36+ // LISTEN = 1,
37+ // SYN_SENT = 2,
38+ // SYN_RCVD = 3,
39+ // ESTABLISHED = 4,
40+ // FIN_WAIT_1 = 5,
41+ // FIN_WAIT_2 = 6,
42+ // CLOSE_WAIT = 7,
43+ // CLOSING = 8,
44+ // LAST_ACK = 9,
45+ // TIME_WAIT = 10
46+ // };
47+
48+ assert (server1.state () == tcp_state::CLOSED);
49+ assert (server2.state () == tcp_state::CLOSED);
50+
51+ server1.begin ();
52+
53+ assert (server1.state () == tcp_state::LISTEN);
54+ assert (server2.state () == tcp_state::CLOSED);
55+
56+ server2.begin ();
57+
58+ assert (server1.state () == tcp_state::LISTEN);
59+ assert (server2.state () == tcp_state::CLOSED);
60+
61+ Serial.println (" Done!" );
62+ }
63+
64+ void loop () {
65+ delay (100 );
66+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ src_dir = examples/PerfTests
2525; src_dir = examples/ResumableDownload
2626; src_dir = examples/Rewrite
2727; src_dir = examples/ServerSentEvents
28+ ; src_dir = examples/ServerState
2829; src_dir = examples/SkipServerMiddleware
2930; src_dir = examples/SlowChunkResponse
3031; src_dir = examples/StaticFile
Original file line number Diff line number Diff line change 44#ifndef _ESPAsyncWebServer_H_
55#define _ESPAsyncWebServer_H_
66
7- #include " Arduino.h"
7+ #include < Arduino.h>
8+ #include < FS.h>
9+ #include < lwip/tcpbase.h>
810
9- #include " FS.h"
1011#include < algorithm>
1112#include < deque>
1213#include < functional>
@@ -1075,6 +1076,15 @@ class AsyncWebServer : public AsyncMiddlewareChain {
10751076 void begin ();
10761077 void end ();
10771078
1079+ tcp_state state () const {
1080+ #if defined(ESP8266) || defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
1081+ // ESPAsyncTCP and RPAsyncTCP methods are not corrected declared with const for immutable ones.
1082+ return static_cast <tcp_state>(const_cast <AsyncWebServer *>(this )->_server .status ());
1083+ #else
1084+ return static_cast <tcp_state>(_server.status ());
1085+ #endif
1086+ }
1087+
10781088#if ASYNC_TCP_SSL_ENABLED
10791089 void onSslFileRequest (AcSSlFileHandler cb, void *arg);
10801090 void beginSecure (const char *cert, const char *private_key_file, const char *password);
You can’t perform that action at this time.
0 commit comments