Skip to content

Commit def271e

Browse files
miqmagobitgenoma
authored and
bitgenoma
committed
feat: Server Side Events (SSE)
1 parent cbcba53 commit def271e

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

libraries/WebServer/src/WebServer.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ void WebServer::handleClient() {
314314
_contentLength = CONTENT_LENGTH_NOT_SET;
315315
_handleRequest();
316316

317+
if (_currentClient.isSSE()) {
318+
_currentStatus = HC_WAIT_CLOSE;
319+
_statusChange = millis();
320+
keepCurrentClient = true;
321+
}
317322
// Fix for issue with Chrome based browsers: https://github.com/espressif/arduino-esp32/issues/3652
318323
// if (_currentClient.connected()) {
319324
// _currentStatus = HC_WAIT_CLOSE;
@@ -329,6 +334,10 @@ void WebServer::handleClient() {
329334
}
330335
break;
331336
case HC_WAIT_CLOSE:
337+
if (_currentClient.isSSE()) {
338+
// Never close connection
339+
_statusChange = millis();
340+
}
332341
// Wait for client to close the connection
333342
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
334343
keepCurrentClient = true;

libraries/WiFi/src/WiFiClient.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class WiFiClientSocketHandle {
174174
}
175175
};
176176

177-
WiFiClient::WiFiClient():_connected(false),next(NULL)
177+
WiFiClient::WiFiClient():_connected(false),_sse(false),next(NULL)
178178
{
179179
}
180180

@@ -321,7 +321,7 @@ int WiFiClient::setOption(int option, int *value)
321321

322322
int WiFiClient::getOption(int option, int *value)
323323
{
324-
socklen_t size = sizeof(int);
324+
socklen_t size = sizeof(int);
325325
int res = getsockopt(fd(), IPPROTO_TCP, option, (char *)value, &size);
326326
if(res < 0) {
327327
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
@@ -608,3 +608,13 @@ int WiFiClient::fd() const
608608
}
609609
}
610610

611+
void WiFiClient::setSSE(bool sse)
612+
{
613+
_sse = sse;
614+
}
615+
616+
bool WiFiClient::isSSE()
617+
{
618+
return _sse;
619+
}
620+

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

4647
public:
4748
WiFiClient *next;
@@ -63,6 +64,8 @@ class WiFiClient : public ESPLwIPClient
6364
void flush();
6465
void stop();
6566
uint8_t connected();
67+
void setSSE(bool sse);
68+
bool isSSE();
6669

6770
operator bool()
6871
{

0 commit comments

Comments
 (0)