From ac2fb65cb5c2793b9a12db634a53123328e0d899 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Wed, 20 Nov 2024 20:25:44 +0100 Subject: [PATCH] Add sample --- examples/Issue162/Issue162.ino | 76 ++++++++++++++++++++++++++++++++++ platformio.ini | 3 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 examples/Issue162/Issue162.ino diff --git a/examples/Issue162/Issue162.ino b/examples/Issue162/Issue162.ino new file mode 100644 index 0000000..59232b2 --- /dev/null +++ b/examples/Issue162/Issue162.ino @@ -0,0 +1,76 @@ +/** + * + * Connect to AP and run in bash: + * + * > while true; do echo "PING"; sleep 0.1; done | websocat ws://192.168.4.1/ws + * + */ +#include +#ifdef ESP8266 + #include +#endif +#ifdef ESP32 + #include +#endif +#include + +#include +AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); + +void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) { + if (type == WS_EVT_CONNECT) { + Serial.printf("Client #%" PRIu32 " connected.\n", client->id()); + } else if (type == WS_EVT_DISCONNECT) { + Serial.printf("Client #%" PRIu32 " disconnected.\n", client->id()); + } +} + +void setup() { + Serial.begin(115200); + + WiFi.mode(WIFI_AP); + WiFi.softAP("esp-captive"); + + ws.onEvent(onWsEvent); + server.addHandler(&ws); + + server.on("/close_all_ws_clients", HTTP_GET | HTTP_POST, [](AsyncWebServerRequest* request) { + Serial.println("Closing all WebSocket clients..."); + ws.closeAll(); + request->send(200, "application/json", "{\"status\":\"all clients closed\"}"); + }); + + server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { + request->send(200, "text/html", R"rawliteral( + + + + + +

+ + + )rawliteral"); + }); + + server.begin(); +} + +void loop() { + vTaskDelete(NULL); +} diff --git a/platformio.ini b/platformio.ini index 14a2b37..328f427 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,11 +1,12 @@ [platformio] default_envs = arduino-2, arduino-3, arduino-310, esp8266, raspberrypi lib_dir = . -src_dir = examples/CaptivePortal +; src_dir = examples/CaptivePortal ; src_dir = examples/SimpleServer ; src_dir = examples/StreamFiles ; src_dir = examples/Filters ; src_dir = examples/Issue85 +src_dir = examples/Issue162 [env] framework = arduino