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.

Share Improve this question edited Dec 12, 2024 at 7:13 Avatar asked Nov 21, 2024 at 7:32 AvatarAvatar 15.2k11 gold badges136 silver badges217 bronze badges 5
  • Have you tried using the error_event? Tbh I'm not sure if being offline even triggers it. – A-Tech Commented Nov 21, 2024 at 7:51
  • Just tried sse_source.error = function(e) {}; it does not get triggered. – Avatar Commented Nov 21, 2024 at 7:54
  • Nice to see that you found a way, but please add the solution as an answer to your post instead of part of your question (See also this article). – A-Tech Commented Nov 21, 2024 at 12:49
  • I am still testing. I am not sure if this will work in all cases. I give feedback in a few days. – Avatar Commented Nov 21, 2024 at 23:00
  • 1 If you found the solution yourself you can add it as an answer to the question. That will make it easier to find the solution for everyone else. – chrwahl Commented Dec 11, 2024 at 20:47
Add a comment  | 

1 Answer 1

Reset to default 0

I 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;
}

本文标签: