Cannot connect from the same client twice #473

Closed
pghalliday opened this Issue Oct 3, 2012 · 2 comments

Comments

Projects
None yet
4 participants

I ran into this while trying to write unit tests for a socket io based application. I think sample code probably explains it best

var io = require('socket.io');
var io_client = require('socket.io-client');
var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello, world!');
});

var serverSocket = io.listen(server);

server.listen(8080, function() {
  var clientSocket1 = io_client.connect('http://localhost:8080');
  clientSocket1.on('connect', function() {
    console.log('clientSocket1 connected');
    clientSocket1.on('disconnect', function() {
      console.log('clientSocket1 disconnected');
      var clientSocket2 = io_client.connect('http://localhost:8080');
      clientSocket2.on('connect', function() {
        console.log('clientSocket2 connected');
        clientSocket2.on('disconnect', function() {
          console.log('clientSocket2 disconnected');
          server.close(function() {
            console.log('Phew, everything\'s OK');
          });
        });
        clientSocket2.disconnect();
      });
    });
    clientSocket1.disconnect();
  });
});

This results in the following console output

   info  - socket.io started
   debug - client authorized
   info  - handshake authorized 4HJrvg4Tg44Aqg3sf3G_
   debug - setting request GET /socket.io/1/websocket/4HJrvg4Tg44Aqg3sf3G_
   debug - set heartbeat interval for client 4HJrvg4Tg44Aqg3sf3G_
   debug - client authorized for 
   debug - websocket writing 1::
clientSocket1 connected
clientSocket1 disconnected
   debug - got disconnection packet
   info  - transport end by forced client disconnection
   debug - websocket writing 0::
   info  - transport end (booted)
   debug - set close timeout for client 4HJrvg4Tg44Aqg3sf3G_
   debug - cleared close timeout for client 4HJrvg4Tg44Aqg3sf3G_
   debug - cleared heartbeat interval for client 4HJrvg4Tg44Aqg3sf3G_
   debug - discarding transport

and then it hangs and the the second socket never connects, doesn't really look like it even tried

I've encountered this bug too. It also seems to happens if the connection fails (i.e. 'connect_failed' event occurs).
This is a big problem if a client is trying to connect to a server which is not online at a particular time but may want to try again later.

I had the same issue while creating some feature tests using mocha and figured out that if you add the following options it should start working.

io_client.connect('http://localhost:8080', { 'force new connection': true });

Hope this helps!

rauchg closed this Nov 25, 2014

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