I am working with SSE and realized that the browser always goes to its error page if the internet connection gets lost. In the moment of losing the connection it shows “This site can’t be reached”, then a moment later: “No Internet – ERR_INTERNET_DISCONNECTED”.
I am certain that the cause is the SSE connection because all other sites on my server without SSE get loaded and keep showing even if the internet connection is lost.
With Javascript I tried to catch the lost connection with:
sse_source.onerror = function(e)
{
e.preventDefault(); // prevent default, not working
// close SSE source
sse_source.close();
// alert shows up and interrupts, but after clicking Okay the browser error page is displayed
alert('Connection to Server lost. No Live Updates.');
}
But it does nothing.
I also tried:
window.addEventListener('offline', updateOnlineStatus);
function updateOnlineStatus(e)
{
e.preventDefault(); // prevent default, not working
sse_source.close();
alert('Connection to Server lost. No Live Updates.');
}
Here it does not display the alert. The browser always goes to its error page.
Is there any other Javascript to keep showing the website?
Updates:
I can see in the console that the sse_source.onerror
is still processed completely when the connection is lost. But cannot stop the error page from showing up from there.
After the sse_source.onerror
the window.addEventListener('offline', updateOnlineStatus)
is processed. Cannot stop error page from here either.
Ah, it actually shows the alert() with sse_source.onerror
. I updated the first code block.