Skip to content

Commit c21a8cd

Browse files
lucasssvazmiqmago
andauthored
Server Side Events (#9222)
* feat: Server Side Events (SSE) * Update libraries/WiFi/src/WiFiClient.cpp Co-authored-by: Lucas Saavedra Vaz <[email protected]> --------- Co-authored-by: Miquel Martin <[email protected]> Co-authored-by: Miquel <[email protected]>
1 parent aceea3e commit c21a8cd

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Diff for: libraries/WebServer/src/WebServer.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ void WebServer::handleClient() {
402402
_contentLength = CONTENT_LENGTH_NOT_SET;
403403
_handleRequest();
404404

405+
if (_currentClient.isSSE()) {
406+
_currentStatus = HC_WAIT_CLOSE;
407+
_statusChange = millis();
408+
keepCurrentClient = true;
409+
}
405410
// Fix for issue with Chrome based browsers: https://github.com/espressif/arduino-esp32/issues/3652
406411
// if (_currentClient.connected()) {
407412
// _currentStatus = HC_WAIT_CLOSE;
@@ -417,6 +422,10 @@ void WebServer::handleClient() {
417422
}
418423
break;
419424
case HC_WAIT_CLOSE:
425+
if (_currentClient.isSSE()) {
426+
// Never close connection
427+
_statusChange = millis();
428+
}
420429
// Wait for client to close the connection
421430
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
422431
keepCurrentClient = true;

Diff for: libraries/WiFi/src/WiFiClient.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class WiFiClientSocketHandle {
187187
}
188188
};
189189

190-
WiFiClient::WiFiClient():_rxBuffer(nullptr),_connected(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
190+
WiFiClient::WiFiClient():_rxBuffer(nullptr),_connected(false),_sse(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
191191
{
192192
}
193193

@@ -347,7 +347,7 @@ int WiFiClient::setOption(int option, int *value)
347347

348348
int WiFiClient::getOption(int option, int *value)
349349
{
350-
socklen_t size = sizeof(int);
350+
socklen_t size = sizeof(int);
351351
int res = getsockopt(fd(), IPPROTO_TCP, option, (char *)value, &size);
352352
if(res < 0) {
353353
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
@@ -661,3 +661,14 @@ int WiFiClient::fd() const
661661
return clientSocketHandle->fd();
662662
}
663663
}
664+
665+
void WiFiClient::setSSE(bool sse)
666+
{
667+
_sse = sse;
668+
}
669+
670+
bool WiFiClient::isSSE()
671+
{
672+
return _sse;
673+
}
674+

Diff for: libraries/WiFi/src/WiFiClient.h

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class WiFiClient : public ESPLwIPClient
4242
std::shared_ptr<WiFiClientSocketHandle> clientSocketHandle;
4343
std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
4444
bool _connected;
45+
bool _sse;
4546
int _timeout;
4647
int _lastWriteTimeout;
4748
int _lastReadTimeout;
@@ -66,6 +67,8 @@ class WiFiClient : public ESPLwIPClient
6667
void flush();
6768
void stop();
6869
uint8_t connected();
70+
void setSSE(bool sse);
71+
bool isSSE();
6972

7073
operator bool()
7174
{

0 commit comments

Comments
 (0)