Skip to content
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

Network disconnect / offline mode not detected #1433

Closed
hyperknot opened this issue Jan 3, 2021 · 5 comments
Closed

Network disconnect / offline mode not detected #1433

hyperknot opened this issue Jan 3, 2021 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@hyperknot
Copy link

Describe the bug
When turning off the wireless network on my MacBook, the client does not detect the disconnect event. It waits a possibly long time until the ping detects it. Using Websockets.

To Reproduce
Any basic example will replicate it, for example https://socket.io/demos/chat/ replicates it.

Server
Does not matter, it is disconnected on the client.

Socket.IO client version: 3.0.4

Client

Default options, only auth is specified in options.

Expected behavior
A clear and concise description of what you expected to happen.

When disconnecting the network, the socket should detect a disconnect event.
The browser correctly recognizes such event, the following triggers immediately.

window.addEventListener('offline', () => {alert('offline')});

navigator.onLine is also correctly showing false.

However SocketIO client believes it is still connected, the WS connection is sending messages like everything was connected. Only after the next ping event it recognizes that something is wrong and fires the disconnect event.

Platform:

  • Device: Macbook Pro
  • OS: macOS 10.15.7
  • Browser: latest Chrome and Firefox
@hyperknot
Copy link
Author

As a workaround, is there a way to force-send a ping event?

@hyperknot
Copy link
Author

So far I came up with this workaround, this seems to work.

    window.addEventListener('offline', () => {
      if (
        this.socket &&
        this.socket.io &&
        this.socket.io.engine &&
        this.socket.io.engine.transport
      ) {
        this.socket.io.engine.transport.close()
      }
    })

How do everyone else handle this problem?

@dverzolla
Copy link

@darrachequesne
Copy link
Member

@dverzolla I don't think this is fixed yet.

@hyperknot I could indeed reproduce, thanks. That's really weird, one could expect that the TCP connection would be closed when the network is turned off, but that does not seem to be the case.

darrachequesne added a commit to socketio/engine.io-client that referenced this issue Mar 2, 2021
The connection will be closed once the "offline" event is emitted by
the browser, in order not to wait for the heartbeat mechanism to detect
the disconnection.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event

Related: socketio/socket.io-client#1433
@darrachequesne
Copy link
Member

This should be fixed by socketio/engine.io-client@c361bc6, included in [email protected].

Thanks for the detailed bug report 👍

@darrachequesne darrachequesne added this to the 4.0.0 milestone Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants