title: in nodejs when i enter two player in a room players disconnected
body: <p>I want to create online tic tac with unity and nodejs that in it two player enter a room automatically when i enter two player in a room players disconnected!!
  Note:each room has two player maximum </p>
  
  <p>problems :</p>
  
  <p>players  disconnected immediately after connect</p>
  
  <p>and</p>
  
  <p>io.sockets.clients().length  is not working</p>
  
  <pre><code>var io = require('socket.io')(process.env.PORT || 3000);
  var shortid = require('shortid');
  console.log('server started');
  var usernames = {};
  var ids = {};
  var rooms = [];
  var roomExist = false;//true if a room exist with one player in there
  var lastRoom ;  // id of room  with one player
  var numClients = 0;
  io.sockets.on('connection',function(socket){
      socket.on('addUser',function(username){
          socket.username = username;
      });
      socket.id = shortid.generate();
      socket.emit('yourId',socket.id);
      if (roomExist){//Player 2 joined
          socket.room = lastRoom;
          socket.join(lastRoom);
          roomExist = false;
          console.log('J client by id' ,socket.id,'by name',socket.username,'joined room',socket.room);
          console.log('number of clients is :',io.sockets.clients().length);
          socket.broadcast.to(socket.room).emit('spawn');
          socket.on('move',function(data){
              console.log('client moved',JSON.stringify(data));
              socket.broadcast.to(socket.room).emit('move',data);
          });
      }
      else {//Player 1 joined
          lastRoom = shortid.generate();
          rooms.push(lastRoom);
          socket.room = lastRoom;
          socket.join(lastRoom);
          roomExist = true;
          console.log('C client by id' ,socket.id,'by name',socket.username,'joined room',socket.room);
          console.log('number of clients is :',io.sockets.clients().length);
          socket.emit('wait'); // wait for player 2
          socket.on('move',function(data){
              console.log('client moved',JSON.stringify(data));
              socket.broadcast.to(socket.room).emit('move',data);
          });
      }
      socket.on('disconnect',function(){
          delete usernames[socket.username];
          delete usernames[socket.username];
          console.log('client',socket.username,'disconnected');
          io.sockets.emit('updateusers', usernames);
          socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
          socket.leave(socket.room);
      });
  });
  </code></pre>