From def271ea9ed1a621b8a5ece55890b45a7b8665d6 Mon Sep 17 00:00:00 2001 From: Miquel Martin Date: Sun, 11 Jul 2021 00:46:31 +0200 Subject: [PATCH 1/2] feat: Server Side Events (SSE) --- libraries/WebServer/src/WebServer.cpp | 9 +++++++++ libraries/WiFi/src/WiFiClient.cpp | 14 ++++++++++++-- libraries/WiFi/src/WiFiClient.h | 3 +++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index 9f29ff9d2c9..7216de5bafe 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -314,6 +314,11 @@ void WebServer::handleClient() { _contentLength = CONTENT_LENGTH_NOT_SET; _handleRequest(); + if (_currentClient.isSSE()) { + _currentStatus = HC_WAIT_CLOSE; + _statusChange = millis(); + keepCurrentClient = true; + } // Fix for issue with Chrome based browsers: https://github.com/espressif/arduino-esp32/issues/3652 // if (_currentClient.connected()) { // _currentStatus = HC_WAIT_CLOSE; @@ -329,6 +334,10 @@ void WebServer::handleClient() { } break; case HC_WAIT_CLOSE: + if (_currentClient.isSSE()) { + // Never close connection + _statusChange = millis(); + } // Wait for client to close the connection if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) { keepCurrentClient = true; diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index 17831d3b3e5..b4842cc2924 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -174,7 +174,7 @@ class WiFiClientSocketHandle { } }; -WiFiClient::WiFiClient():_connected(false),next(NULL) +WiFiClient::WiFiClient():_connected(false),_sse(false),next(NULL) { } @@ -321,7 +321,7 @@ int WiFiClient::setOption(int option, int *value) int WiFiClient::getOption(int option, int *value) { - socklen_t size = sizeof(int); + socklen_t size = sizeof(int); int res = getsockopt(fd(), IPPROTO_TCP, option, (char *)value, &size); if(res < 0) { log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno)); @@ -608,3 +608,13 @@ int WiFiClient::fd() const } } +void WiFiClient::setSSE(bool sse) +{ + _sse = sse; +} + +bool WiFiClient::isSSE() +{ + return _sse; +} + diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h index 4915cfd5203..7d23ae99e0d 100644 --- a/libraries/WiFi/src/WiFiClient.h +++ b/libraries/WiFi/src/WiFiClient.h @@ -42,6 +42,7 @@ class WiFiClient : public ESPLwIPClient std::shared_ptr clientSocketHandle; std::shared_ptr _rxBuffer; bool _connected; + bool _sse; public: WiFiClient *next; @@ -63,6 +64,8 @@ class WiFiClient : public ESPLwIPClient void flush(); void stop(); uint8_t connected(); + void setSSE(bool sse); + bool isSSE(); operator bool() { From 0dcc1f24ec1f0186676f213268e8e6f0cd3f2f08 Mon Sep 17 00:00:00 2001 From: Miquel Date: Mon, 5 Feb 2024 17:13:05 +0100 Subject: [PATCH 2/2] Update libraries/WiFi/src/WiFiClient.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/WiFi/src/WiFiClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index d490eb33f36..7d4def40d3e 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -175,7 +175,7 @@ class WiFiClientSocketHandle { } }; -WiFiClient::WiFiClient():_connected(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),_sse(false),next(NULL) +WiFiClient::WiFiClient():_connected(false),_sse(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL) { }