|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov |
| 3 | + |
| 4 | +// |
| 5 | +// SSE 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 const char *htmlContent PROGMEM = R"( |
| 23 | +<!DOCTYPE html> |
| 24 | +<html> |
| 25 | +<head> |
| 26 | + <title>Server-Sent Events</title> |
| 27 | + <script> |
| 28 | + if (!!window.EventSource) { |
| 29 | + var source = new EventSource('/events'); |
| 30 | + source.onopen = function(e) { |
| 31 | + console.log("Events Connected"); |
| 32 | + }; |
| 33 | + source.onerror = function(e) { |
| 34 | + if (e.target.readyState != EventSource.OPEN) { |
| 35 | + console.log("Events Disconnected"); |
| 36 | + } |
| 37 | + // Uncomment below to prevent the client from proactively establishing a new connection. |
| 38 | + // source.close(); |
| 39 | + }; |
| 40 | + source.onmessage = function(e) { |
| 41 | + console.log("Message: " + e.data); |
| 42 | + }; |
| 43 | + source.addEventListener('heartbeat', function(e) { |
| 44 | + console.log("Heartbeat", e.data); |
| 45 | + }, false); |
| 46 | + } |
| 47 | + </script> |
| 48 | +</head> |
| 49 | +<body> |
| 50 | + <h1>Open your browser console!</h1> |
| 51 | +</body> |
| 52 | +</html> |
| 53 | +)"; |
| 54 | + |
| 55 | +static const size_t htmlContentLength = strlen_P(htmlContent); |
| 56 | + |
| 57 | +static AsyncWebServer server(80); |
| 58 | +static AsyncEventSource events("/events"); |
| 59 | + |
| 60 | +static volatile size_t connectionCount = 0; |
| 61 | +static volatile uint32_t timestampConnected = 0; |
| 62 | +static constexpr uint32_t timeoutClose = 15000; |
| 63 | + |
| 64 | +void setup() { |
| 65 | + Serial.begin(115200); |
| 66 | + |
| 67 | +#ifndef CONFIG_IDF_TARGET_ESP32H2 |
| 68 | + WiFi.mode(WIFI_AP); |
| 69 | + WiFi.softAP("esp-captive"); |
| 70 | +#endif |
| 71 | + |
| 72 | + // curl -v http://192.168.4.1/ |
| 73 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { |
| 74 | + // need to cast to uint8_t* |
| 75 | + // if you do not, the const char* will be copied in a temporary String buffer |
| 76 | + request->send(200, "text/html", (uint8_t *)htmlContent, htmlContentLength); |
| 77 | + }); |
| 78 | + |
| 79 | + events.onConnect([](AsyncEventSourceClient *client) { |
| 80 | + /** |
| 81 | + * @brief: Purpose for a test case: count() function |
| 82 | + * Task watchdog shall be triggered due to a self-deadlock by mutex handling of the AsyncEventSource. |
| 83 | + * |
| 84 | + * E (61642) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time: |
| 85 | + * E (61642) task_wdt: - async_tcp (CPU 0/1) |
| 86 | + * |
| 87 | + * Resolve: using recursive_mutex insteads of mutex. |
| 88 | + */ |
| 89 | + connectionCount = events.count(); |
| 90 | + |
| 91 | + timestampConnected = millis(); |
| 92 | + Serial.printf("SSE Client connected! ID: %" PRIu32 "\n", client->lastId()); |
| 93 | + client->send("hello!", NULL, millis(), 1000); |
| 94 | + Serial.printf("Number of connected clients: %u\n", connectionCount); |
| 95 | + }); |
| 96 | + |
| 97 | + events.onDisconnect([](AsyncEventSourceClient *client) { |
| 98 | + connectionCount = events.count(); |
| 99 | + Serial.printf("SSE Client disconnected! ID: %" PRIu32 "\n", client->lastId()); |
| 100 | + Serial.printf("Number of connected clients: %u\n", connectionCount); |
| 101 | + }); |
| 102 | + |
| 103 | + server.addHandler(&events); |
| 104 | + |
| 105 | + server.begin(); |
| 106 | +} |
| 107 | + |
| 108 | +static constexpr uint32_t deltaSSE = 3000; |
| 109 | +static uint32_t lastSSE = 0; |
| 110 | +static uint32_t lastHeap = 0; |
| 111 | + |
| 112 | +void loop() { |
| 113 | + uint32_t now = millis(); |
| 114 | + if (connectionCount > 0) { |
| 115 | + if (now - lastSSE >= deltaSSE) { |
| 116 | + events.send(String("ping-") + now, "heartbeat", now); |
| 117 | + lastSSE = millis(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief: Purpose for a test case: close() function |
| 122 | + * Task watchdog shall be triggered due to a self-deadlock by mutex handling of the AsyncEventSource. |
| 123 | + * |
| 124 | + * E (61642) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time: |
| 125 | + * E (61642) task_wdt: - async_tcp (CPU 0/1) |
| 126 | + * |
| 127 | + * Resolve: using recursive_mutex insteads of mutex. |
| 128 | + */ |
| 129 | + if (now - timestampConnected >= timeoutClose) { |
| 130 | + Serial.printf("SSE Clients close\n"); |
| 131 | + events.close(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | +#ifdef ESP32 |
| 136 | + if (now - lastHeap >= 2000) { |
| 137 | + Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); |
| 138 | + lastHeap = now; |
| 139 | + } |
| 140 | +#endif |
| 141 | +} |
0 commit comments