File tree Expand file tree Collapse file tree 3 files changed +79
-2
lines changed Expand file tree Collapse file tree 3 files changed +79
-2
lines changed 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
25
25
; src_dir = examples/ResumableDownload
26
26
; src_dir = examples/Rewrite
27
27
; src_dir = examples/ServerSentEvents
28
+ ; src_dir = examples/ServerState
28
29
; src_dir = examples/SkipServerMiddleware
29
30
; src_dir = examples/SlowChunkResponse
30
31
; src_dir = examples/StaticFile
Original file line number Diff line number Diff line change 4
4
#ifndef _ESPAsyncWebServer_H_
5
5
#define _ESPAsyncWebServer_H_
6
6
7
- #include " Arduino.h"
7
+ #include < Arduino.h>
8
+ #include < FS.h>
9
+ #include < lwip/tcpbase.h>
8
10
9
- #include " FS.h"
10
11
#include < algorithm>
11
12
#include < deque>
12
13
#include < functional>
@@ -1075,6 +1076,15 @@ class AsyncWebServer : public AsyncMiddlewareChain {
1075
1076
void begin ();
1076
1077
void end ();
1077
1078
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
+
1078
1088
#if ASYNC_TCP_SSL_ENABLED
1079
1089
void onSslFileRequest (AcSSlFileHandler cb, void *arg);
1080
1090
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