i am trying to sent server events from esp8266WebServer to a connected browser. The javascript code registers to the eventsource, receives the data and shows it in the corresponding div-tag, but it seems like a connection drop after that, so that the browser repeatly reconnects to the source and throws an event error at onmessage(). It seems, that the server code is not able to hold a persistent connection over which the event could be sent. if i call the function that sends the event message nothing happens on the browser. i examined the esp8266WebServer.sent() function and found out, that it always sends a Connection: close header, so i build the keep-alive header and content myself. Browser now reflects that responseheader. i removed the Content.length as read, that could be a problem to firefox. I also tried ios safari browser as firefox could cause the error, but the result is the same.
i´m trying for hours to get SSEs to work - maybe some of you have a good advice for me?
I am using arduino ide 1.61 with esp8266 core sdk version 1.3.0.
parts of the used code:
ESP8266WebServer webserver(80);
webserver.on("/rfmonitor", rfmonitor);
webserver.on("/rfevent", rfevent);
webserver.on("/eventcheck.js", [](){if(debug) Serial.println(F("eventcheck.js")); webserver.send(200, "text/plain", eventcheck_js);});
//////////////////////////////////////////////////////////////////////////////////////////////////
// send monitoring page with ServerSentEvent support
void rfmonitor(){
if (debug) Serial.println(__FUNCTION__);
String Header, result;
result = "<head> <meta content='text/html'; charset='utf-8'/>";
result += "<meta name='viewport' content='width=device-width, initial-scale=1'/>";
result += "<title>RF-Monitor</title></head><h1>RF-Monitor</h1>";
result += "<body><div id='rfoutput'></div>";
result += "<script src='/eventcheck.js'></script></body>";
Header = "HTTP/1.1 200 OK\r\n";
Header += "Content-Type: text/html;charset=UTF-8\r\n";
Header += "Connection: keep-alive\r\n";
Header += "Cache-Control: no-cache\r\n";
// Header += "Content-Length: " + result.length() + "\r\n";
Header += "Access-Control-Allow-Origin: *\r\n";
Header += "\r\n";
webserver.sendContent(Header);
webserver.sendContent(result);
// webserver.send(200, "text/html", result);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// send event data
void rfevent(){
if (debug) Serial.println(__FUNCTION__);
String Header, result;
// result = "data: {'eventName': 'rf_received', ";
result = "data: {uptime: " + String(uptime()) + "}\n\n";
// result += "event: message \n";
result += "retry: 10000\n";
Header = "HTTP/1.1 200 OK\r\n";
Header += "Content-Type: text/event-stream;charset=UTF-8\r\n";
Header += "Connection: keep-alive\r\n";
Header += "Cache-Control: no-cache\r\n";
// Header += "Content-Length: " + result.length() + "\r\n";
Header += "Access-Control-Allow-Origin: *\r\n";
Header += "\r\n";
webserver.sendContent(Header);
webserver.sendContent(result);
}
const char eventcheck_js[] PROGMEM = R"=====(
if (typeof(EventSource) != "undefined"){
var source = new EventSource('/rfevent');
var outputElement = document.getElementById('rfoutput');
var eventCounter = 0;
source.onmessage = function(e) {
eventCounter++;
outputElement.innerHTML = "data:" + e.data + " <br> " + eventCounter + " Events";
console.log(e.data);
};
source.addEventListener('open', function(e) {
console.log("connected");
}, false);
source.addEventListener('error', function(e) {
console.error(e);
}, false);
}
else{
document.write("This Browser does not support EventSource.")
}
)=====";
this is the console.log from firefox
connected eventcheck.js:12:4
{uptime: 0 days - 00:16:09} eventcheck.js:9:7
error { target: EventSource, isTrusted: true, currentTarget: EventSource, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, timeStamp: 1456434943116000, originalTarget: EventSource, explicitOriginalTarget: EventSource, NONE: 0 } eventcheck.js:15:4
connected eventcheck.js:12:4
{uptime: 0 days - 00:16:22} eventcheck.js:9:7
error { target: EventSource, isTrusted: true, currentTarget: EventSource, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, timeStamp: 1456434955663000, originalTarget: EventSource, explicitOriginalTarget: EventSource, NONE: 0 } eventcheck.js:15:4
this is the serial log
rfmonitor
eventcheck.js
rfevent
rfevent
rfevent
rfevent
...