Socket.io takes a long time before triggering the

2019-07-09 04:11发布

问题:

I'm doing an HTML5 Game using node.js and socket.io

I decided to host it on Heroku.

Heroku isn't allowing the use of WebSockets, so I have to setup xhr-polling instead. (Socket-io on Heroku)

io.configure( function() {
    io.set( "transports", ["xhr-polling"] );
    io.set( "polling duration", 10 );
} );

Before, I was using web-sockets only

io.set( "transports", ["websocket"] );

Now, when a client disconnect (close his window or refresh his page) the event "disconnect" isn't trigger immediatly on the server (it looks like it's waiting for the client heartbeat to time out).

client.on( "disconnect", onClientDisconnect );

If the client reloads, I get multiple connection events before disconnect is fired. My problem is here.

Do you have any ideas, why xhr-polling doesn't fire the disconnect event ? Is this a bad configuration of socket.io ?

Thanks.

回答1:

It says here that you can configure the heartbeat. To properly configure it, you must adjust the heartbeat both on the server and the client side (which is given here).

Try lowering the heartbeat. It may solve your problem. On other note, appfog seems to support websockets.



回答2:

Just configure session auth and you can always know what client has connected. E.g.

io.set('authorization', function(handshakeData, ack) {
    var cookies = require(...);
    var signedCookies = parseCookies(cookies, secret);
    sessionStore.get(signedCookies['connect.sid'], function(err, sessionData) {
        handshakeData.session = sessionData || {};
        handshakeData.sid = signedCookies['connect.sid'] || null;
        ack(err, err ? false : true);
    });
});