summaryrefslogtreecommitdiff
path: root/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ESP_Async_WebServer/idf_component_examples/websocket/main')
-rw-r--r--libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt2
-rw-r--r--libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml6
-rw-r--r--libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp102
3 files changed, 110 insertions, 0 deletions
diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt
new file mode 100644
index 0000000..9eb7ec4
--- /dev/null
+++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt
@@ -0,0 +1,2 @@
+idf_component_register(SRCS "main.cpp"
+ INCLUDE_DIRS ".")
diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml
new file mode 100644
index 0000000..e2d1c65
--- /dev/null
+++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml
@@ -0,0 +1,6 @@
+## IDF Component Manager Manifest File
+dependencies:
+ esp32async/espasyncwebserver:
+ version: "*"
+ override_path: "../../../"
+ pre_release: true
diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp
new file mode 100644
index 0000000..843d1a4
--- /dev/null
+++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
+
+//
+// WebSocket example
+//
+
+#include <Arduino.h>
+#include <AsyncTCP.h>
+#include <WiFi.h>
+
+#include <ESPAsyncWebServer.h>
+
+static AsyncWebServer server(80);
+static AsyncWebSocket ws("/ws");
+
+void setup() {
+ Serial.begin(115200);
+
+#ifndef CONFIG_IDF_TARGET_ESP32H2
+ WiFi.mode(WIFI_AP);
+ WiFi.softAP("esp-captive");
+#endif
+
+ //
+ // Run in terminal 1: websocat ws://192.168.4.1/ws => should stream data
+ // Run in terminal 2: websocat ws://192.168.4.1/ws => should stream data
+ // Run in terminal 3: websocat ws://192.168.4.1/ws => should fail:
+ //
+ // To send a message to the WebSocket server:
+ //
+ // echo "Hello!" | websocat ws://192.168.4.1/ws
+ //
+ ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
+ (void)len;
+
+ if (type == WS_EVT_CONNECT) {
+ ws.textAll("new client connected");
+ Serial.println("ws connect");
+ client->setCloseClientOnQueueFull(false);
+ client->ping();
+
+ } else if (type == WS_EVT_DISCONNECT) {
+ ws.textAll("client disconnected");
+ Serial.println("ws disconnect");
+
+ } else if (type == WS_EVT_ERROR) {
+ Serial.println("ws error");
+
+ } else if (type == WS_EVT_PONG) {
+ Serial.println("ws pong");
+
+ } else if (type == WS_EVT_DATA) {
+ AwsFrameInfo *info = (AwsFrameInfo *)arg;
+ String msg = "";
+ if (info->final && info->index == 0 && info->len == len) {
+ if (info->opcode == WS_TEXT) {
+ data[len] = 0;
+ Serial.printf("ws text: %s\n", (char *)data);
+ }
+ }
+ }
+ });
+
+ // shows how to prevent a third WS client to connect
+ server.addHandler(&ws).addMiddleware([](AsyncWebServerRequest *request, ArMiddlewareNext next) {
+ // ws.count() is the current count of WS clients: this one is trying to upgrade its HTTP connection
+ if (ws.count() > 1) {
+ // if we have 2 clients or more, prevent the next one to connect
+ request->send(503, "text/plain", "Server is busy");
+ } else {
+ // process next middleware and at the end the handler
+ next();
+ }
+ });
+
+ server.addHandler(&ws);
+
+ server.begin();
+}
+
+static uint32_t lastWS = 0;
+static uint32_t deltaWS = 100;
+
+static uint32_t lastHeap = 0;
+
+void loop() {
+ uint32_t now = millis();
+
+ if (now - lastWS >= deltaWS) {
+ ws.printfAll("kp%.4f", (10.0 / 3.0));
+ lastWS = millis();
+ }
+
+ if (now - lastHeap >= 2000) {
+ // cleanup disconnected clients or too many clients
+ ws.cleanupClients();
+
+ Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap());
+ lastHeap = now;
+ }
+}