Skip to content

feat(esp32): Server Side Events (SSE) #5373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libraries/WebServer/src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,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;
Expand All @@ -331,6 +336,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;
Expand Down
14 changes: 12 additions & 2 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class WiFiClientSocketHandle {
}
};

WiFiClient::WiFiClient():_connected(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
WiFiClient::WiFiClient():_connected(false),_sse(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
{
}

Expand Down Expand Up @@ -339,7 +339,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));
Expand Down Expand Up @@ -626,3 +626,13 @@ int WiFiClient::fd() const
}
}

void WiFiClient::setSSE(bool sse)
{
_sse = sse;
}

bool WiFiClient::isSSE()
{
return _sse;
}

3 changes: 3 additions & 0 deletions libraries/WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WiFiClient : public ESPLwIPClient
std::shared_ptr<WiFiClientSocketHandle> clientSocketHandle;
std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
bool _connected;
bool _sse;
int _timeout;

public:
Expand All @@ -64,6 +65,8 @@ class WiFiClient : public ESPLwIPClient
void flush();
void stop();
uint8_t connected();
void setSSE(bool sse);
bool isSSE();

operator bool()
{
Expand Down