Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disconnect problems #431

Closed
helxsz opened this issue Jul 28, 2011 · 4 comments
Closed

disconnect problems #431

helxsz opened this issue Jul 28, 2011 · 4 comments

Comments

@helxsz
Copy link

helxsz commented Jul 28, 2011

I make a simple example for socket.io. when the client emits the 'disconnect' event, the server side would execute socket.on('disconnect', function(msg) too many times that the server would break down because of ' socket.disconnect(socket.id); '

If I comment that line, there is no errors, so what is the problem ?

code:

var app = require('express').createServer();
var redis = require('redis');
app.listen(3000);
var io = require('socket.io').listen(app);

io.sockets.on('connection',function(socket){
   console.log("Client has connected    id:    "+socket.id+"   ");
  socket.emit('news',{hello:'world'});

  socket.on('disconnect', function(msg)
  {   
      console.log("Client has disconnected    id:    "+socket.id+"   ");
      socket.disconnect(socket.id);   
  }) ;

});
@3rd-Eden
Copy link
Contributor

socket.on(disconnect, fn) tells you that a client has disconnected. So wat you are doing is trying to disconnect a disconnected client, and disconnect triggers the disconnect event again, creating a infinit loop.

@helxsz
Copy link
Author

helxsz commented Jul 28, 2011

I see , but if I don't use socket.disconnect(socket.id); I could not close the connection with client
how can I correct it ?

@3rd-Eden
Copy link
Contributor

The connection is closed automatically when the client leaves the page/refreshes/closes browser etc.

But if you want to close the connection do it outside the disconnect event.

@helxsz
Copy link
Author

helxsz commented Jul 28, 2011

ok, i get it , thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants