Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Timing issue #1668

Closed
fdellabetta opened this Issue · 7 comments

5 participants

@fdellabetta

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
@fdellabetta fdellabetta reopened this
@matthaywardwebdesign

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.

@fdellabetta

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

@fdellabetta

@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

@nkzawa
Owner

@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();
      });
    });
  });
});
@rauchg
Owner

Good find!

@fdellabetta

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

@rase-
Collaborator

Already updated.

@rase- rase- closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.