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

How ban one user? #2147

Closed
martec opened this issue Jun 6, 2015 · 14 comments
Closed

How ban one user? #2147

martec opened this issue Jun 6, 2015 · 14 comments

Comments

@martec
Copy link

martec commented Jun 6, 2015

How ban one user using socket.id in 1.0 series?

@dsalcedo
Copy link

I think you need:

Any way, this is not a issue can you solve this 'question' in http://stackoverflow.com/
Code strong!

@martec
Copy link
Author

martec commented Jul 28, 2015

@dsalcedo
you making some confusion.
I already has socket.id of user. But in 1.0 not work more socket.manager.onClientDisconnect(socket.id).
So what command i can use in instead?

@dsalcedo
Copy link

In this part of code exist :
https://github.com/socketio/socket.io/blob/master/lib/socket.js#L446

/**
 * Disconnects this client.
 *
 * @param {Boolean} if `true`, closes the underlying connection
 * @return {Socket} self
 * @api public
 */
Socket.prototype.disconnect = function(close){
  if (!this.connected) return this;
  if (close) {
    this.client.disconnect();
  } else {
    this.packet({ type: parser.DISCONNECT });
    this.onclose('server namespace disconnect');
  }
  return this;
};

so, i think (i think) this should be work:

/*
* Client side
*/
socket.emit('forceDisconnect');

/*
* Server side
*/
socket.on('forceDisconnect', function(){
    socket.disconnect();
});

Code strong!

@martec
Copy link
Author

martec commented Jul 29, 2015

@dsalcedo
i already tried this before you posted here.
not work correctly. if user try connect after disconnect usinbg code above will appear two message in each message that user send.
socket.manager.onClientDisconnect(socket.id) in 0.9 or below work perfect. but 1.0 not.

@dsalcedo
Copy link

Im reading this post in the blog of Socket.io and found this:
http://socket.io/blog/socket-io-1-2-0/

// Client side
var socket = io({ forceNew: true });
socket.once('connect', function() {
  socket.disconnect();
});

socket.once('disconnect', function() {
  socket.once('connect', function() {
    console.log('Connected for the second time!');
  });
  socket.connect();
});

Code strong !!

@martec
Copy link
Author

martec commented Jul 30, 2015

@dsalcedo
plz make test here first before post.
already tested all thing you saying...
not work correctly.
what i need is socket.manager.onClientDisconnect(socket.id) in 1.0 series.

@dsalcedo
Copy link

you're absolutely right, I've made changes and I disconnect the socket by server side
captura de pantalla 2015-07-29 a las 23 33 59

// Server side
io.on('connection', function (socket) {
  console.log('socketID: ',socket.id, ' - connected')

  setTimeout(function() {
    console.log('socketID: ',socket.id,' - disconnect')
   socket.conn.close();
    console.log('socketID: ',this.socket,' - disconnect')
  }, 3000);
});
// Client side
  var socket = io({
    reconnection:false
  });

@DannyvanderJagt
Copy link

If you want to prevent someone from connecting to your server you should use socket.io middelware.
Middelware is basic stuff in http and it in new in socket.io 1.0. See this migration guide

You can use it like this:

// Server side
var io = require('socket.io')(3000);

io.use(function(socket, next){
   // socket contains a list of headers including the clients ip adress etc.
   if(isUserIsAllowed(socket)){  // Check if the user is banned. (your custom function)
         // An empty next() will allow the use to connect.
        next();
   }else{
       // This will send the client and error event and it will prevent the client from connecting to your server.
       next(new Error('You are not allowed on this server!'));
   }  
});

If you want to disconnect a client which is already connected you simple use:

socket.disconnect();

The client will get a disconnect event. It will still be able to send events to your server but they will never arrive/be processed by your server side code. And the reconnect attempt from the client will fail because of the middelware.

I hope this helps.
The code is tested and is working here! ;) Let me know if you have any problems!

@martec
Copy link
Author

martec commented Jul 31, 2015

@DannyvanderJagt
thanks but i need disconnect specific user using socket.id
because of this that i need something like socket.manager.onClientDisconnect(socket.id) in 1.0

@DannyvanderJagt
Copy link

I think I understand your problem now.
You want to get a socket by its id and disconnect that socket. I'v read that you already have the socket.id of the socket you want to disconnect.

In order to get a socket by socket.id you can use this:

var io = require('socket.io')(3000);

// Get a socket by id.
var socket = io.to(socket.id);

// Now that you have the socket you can do what ever you want. As example you can disconnect it.
socket.disconnect();

Note: The socket can reconnect once it is disconnected. If want to prevent this you can/need to use middelware. See my previous comment. #2147 (comment)

@dsalcedo
Copy link

👍

@martec
Copy link
Author

martec commented Jul 31, 2015

@DannyvanderJagt
@dsalcedo

One of first thing i tried... But not work corectly for me...
has same issue of this user #47 (comment)

@dsalcedo
Copy link

...mmm ok, so maybe you are doing wrong code and you need update to SocketIO V1 with the new methods and events; Remember, the upgrade of socketio to stable version is for make better code and practices. If you find the way for do the action that you are searching, share with us 😄 !

@dsalcedo
Copy link

dsalcedo commented Aug 5, 2015

Please close this question listed as 'issue'. For questions please use stackoverflow.
Code strong.

@nkzawa nkzawa closed this as completed Jan 17, 2016
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

5 participants
@nkzawa @dsalcedo @DannyvanderJagt @martec and others