Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Timing issue #1668

Closed
fdellabetta opened this Issue Jul 7, 2014 · 7 comments

Comments

Projects
None yet
5 participants
Contributor

fdellabetta commented Jul 7, 2014

In this example, socket.io-client disconnect while in on 'connect' callback.
By setting different value on setTimeout call (inside io.use middleware) I get different behaviours.

If that is set to 100, the on 'disconnect' callback (server side) is not called immediately

If that is set to 0 (the same if I call next() without setTimeout) , the on 'disconnect' callback (server side) is called immediately.

If that is set to 2000, the on 'disconnect' callback (server side) is called immediately after 2 seconds

With socket.io version 0.9.16 'disconnect' callback is always called

Could someone explain me why this happens ?
Thanks in advance

Running on ubuntu 14.04 x64 with node 0.10.25 and socket.io 1.0.6.
Server running with node index.js
Client running with browser http://localhost:3456/client

index.js:

    var app=require('express')();
    var fs=require('fs');
    var http=require('http');
    var client=require('socket.io-client');

    var server=app.listen(3456);
    var io = require('socket.io').listen(server); 

    io.use(function(socket,next){
        setTimeout(next,100);
    })

    io.on('connection', function(socket){
        console.log('connected');
        socket.on('disconnect',function(){
            console.log('disconnected');
        });
    });

    app.get('/client',function(req,res){
        var socket = client.connect('http://localhost:3456');
        socket.on('connect',function(){
            console.log('socket.io-client connected');
            socket.disconnect();
        });
        socket.on('disconnect',function(){
            console.log('socket.io-client disconnected');
        });
        res.end();
    })

@fdellabetta fdellabetta closed this Jul 8, 2014

@fdellabetta fdellabetta reopened this Jul 8, 2014

The socket is always being disconnect due to the socket.disconnect being called just after the socket has connected.

The io.use middleware allows to run code after each connection before proceeding. The setTimeout tells socket io to wait for 100 ms before proceeding after a new connection.

Contributor

fdellabetta commented Jul 17, 2014

Ok, but I don't undestrand why in this example,

console.log('disconnected')

is called after 10 seconds (more or less) and not after 100ms

Contributor

fdellabetta commented Jul 17, 2014

@guille I wrote a test that fails:

    it('should not timeout', function(done){
      var srv = http();
      var sio = io(srv);
      srv.listen(function(){
        sio.use(function(socket,next){
            setTimeout(next,100);
        })
        sio.on('connection', function(socket){
            socket.on('disconnect',function(){
                done();
            });
        });
        var socket = client(srv);
        socket.on('connect',function(){
            socket.disconnect();
        });
        socket.on('disconnect',function(){
        });
      });
    });

with 1500 instead of 100 the test succeeds

Contributor

nkzawa commented Jul 17, 2014

@guille @fdellabetta This looks like because engine.io-client can't send the close packet while pausing a transport.

The same timeout happens on the following test too:

it('should close while pausing a transport', function(done){
  var srv = http();
  var sio = io(srv);
  sio.on('connection', function(socket){
    socket.on('disconnect',function(){
      done();
    });
  });
  var socket = client(srv);
  socket.on('connect',function(){
    socket.io.engine.on('upgrading',function(){
      // wait to pause
      process.nextTick(function() {
        socket.disconnect();
      });
    });
  });
});
Contributor

rauchg commented Jul 17, 2014

Good find!

Contributor

fdellabetta commented Oct 2, 2014

@guille please link socket.io to (at least) engine.io-client#b4c1d063d
After that, this issue can be closed..

Contributor

rase- commented Jan 12, 2015

Already updated.

@rase- rase- closed this Jan 12, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment