socket.io xhr-polling disconnect event

后端 未结 1 1394
天涯浪人
天涯浪人 2020-12-20 03:03

I have a socket.io node script with:

socket.on(\'disconnect\', function(data) {    
  console.log(\'disconnect!\');
});

When I connect with

相关标签:
1条回答
  • 2020-12-20 03:25

    Socket.io switches to the xhr-polling transport when you are viewing the page in your iPhone. This might be caused by the configuration of socket.io or because the browser in your iPhone does not (fully) support websockets.

    The xhr-polling implementation in socket.io does not emit disconnect event when the connection is closed, see github issue #431. You can reproduce this issue in your Chrome browser by forcing the socket.io server to use the xhr-polling transport only:

    // the server side
    var io = require('socket.io').listen(httpServer);
    io.set('transports', ['xhr-polling']);
    

    Good news: you can ask socket.io's client to notify the server about disconnect by turning on the sync disconnect on unload flag:

    // the browser (HTML) side
    var socket = io.connect('http://localhost', {
      'sync disconnect on unload': true
    });
    

    Warning: this option can worsen the user experience when the network and/or your server are slow, see this pull request for more information.

    UPDATE

    According to socket.io force a disconnect over XHR-polling, setting sync disconnect on unload might not be enough to fix the problem on iPhone/iPad.

    As you can see in socket.io-client source code, sync disconnect on unload sets up a listener for beforeunload event, which is not supported by iOS Safari according.

    The solution is probably to fix socket.io-client to listen for both unload and pagehide events, because the unload event may not work as expected for back and forward optimization. Use the pageshow and pagehide events instead. [Apple Web Content Guide].

    0 讨论(0)
提交回复
热议问题