Unverified Commit c21a8cda authored by Lucas Saavedra Vaz's avatar Lucas Saavedra Vaz Committed by GitHub

Server Side Events (#9222)

* feat: Server Side Events (SSE)

* Update libraries/WiFi/src/WiFiClient.cpp
Co-authored-by: default avatarLucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>

---------
Co-authored-by: default avatarMiquel Martin <miqmago@gmail.com>
Co-authored-by: default avatarMiquel <miqmago@users.noreply.github.com>
parent aceea3e5
...@@ -402,6 +402,11 @@ void WebServer::handleClient() { ...@@ -402,6 +402,11 @@ void WebServer::handleClient() {
_contentLength = CONTENT_LENGTH_NOT_SET; _contentLength = CONTENT_LENGTH_NOT_SET;
_handleRequest(); _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 // Fix for issue with Chrome based browsers: https://github.com/espressif/arduino-esp32/issues/3652
// if (_currentClient.connected()) { // if (_currentClient.connected()) {
// _currentStatus = HC_WAIT_CLOSE; // _currentStatus = HC_WAIT_CLOSE;
...@@ -417,6 +422,10 @@ void WebServer::handleClient() { ...@@ -417,6 +422,10 @@ void WebServer::handleClient() {
} }
break; break;
case HC_WAIT_CLOSE: case HC_WAIT_CLOSE:
if (_currentClient.isSSE()) {
// Never close connection
_statusChange = millis();
}
// Wait for client to close the connection // Wait for client to close the connection
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) { if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
keepCurrentClient = true; keepCurrentClient = true;
......
...@@ -187,7 +187,7 @@ public: ...@@ -187,7 +187,7 @@ public:
} }
}; };
WiFiClient::WiFiClient():_rxBuffer(nullptr),_connected(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL) WiFiClient::WiFiClient():_rxBuffer(nullptr),_connected(false),_sse(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
{ {
} }
...@@ -347,7 +347,7 @@ int WiFiClient::setOption(int option, int *value) ...@@ -347,7 +347,7 @@ int WiFiClient::setOption(int option, int *value)
int WiFiClient::getOption(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); int res = getsockopt(fd(), IPPROTO_TCP, option, (char *)value, &size);
if(res < 0) { if(res < 0) {
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno)); log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
...@@ -661,3 +661,14 @@ int WiFiClient::fd() const ...@@ -661,3 +661,14 @@ int WiFiClient::fd() const
return clientSocketHandle->fd(); return clientSocketHandle->fd();
} }
} }
void WiFiClient::setSSE(bool sse)
{
_sse = sse;
}
bool WiFiClient::isSSE()
{
return _sse;
}
...@@ -42,6 +42,7 @@ protected: ...@@ -42,6 +42,7 @@ protected:
std::shared_ptr<WiFiClientSocketHandle> clientSocketHandle; std::shared_ptr<WiFiClientSocketHandle> clientSocketHandle;
std::shared_ptr<WiFiClientRxBuffer> _rxBuffer; std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
bool _connected; bool _connected;
bool _sse;
int _timeout; int _timeout;
int _lastWriteTimeout; int _lastWriteTimeout;
int _lastReadTimeout; int _lastReadTimeout;
...@@ -66,6 +67,8 @@ public: ...@@ -66,6 +67,8 @@ public:
void flush(); void flush();
void stop(); void stop();
uint8_t connected(); uint8_t connected();
void setSSE(bool sse);
bool isSSE();
operator bool() operator bool()
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment