admin管理员组文章数量:1332377
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
if (sse_source.readyState == EventSource.CLOSED)
{
// close SSE source
sse_source.close();
}
// alert shows up and interrupts, but after clicking OK
// 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
when I remove the sse_source.readyState == EventSource.CLOSED
. The readyState was 0
.
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
if (sse_source.readyState == EventSource.CLOSED)
{
// close SSE source
sse_source.close();
}
// alert shows up and interrupts, but after clicking OK
// 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
when I remove the sse_source.readyState == EventSource.CLOSED
. The readyState was 0
.
1 Answer
Reset to default 0I solved it finally with simply:
sse_source.onerror = function(e)
{
alert('Connection lost. No Live Updates.');
e.preventDefault();
// close SSE source
sse_source.close();
return;
}
本文标签:
版权声明:本文标题:javascript - How to catch lost internet connection with Server-Sent-Events (SSE) and prevent the browser from showing its error 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742306281a2449982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sse_source.error = function(e) {};
it does not get triggered. – Avatar Commented Nov 21, 2024 at 7:54