-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Comments
I think you need:
Any way, this is not a issue can you solve this 'question' in http://stackoverflow.com/ |
@dsalcedo |
In this part of code exist : /**
* 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! |
@dsalcedo |
Im reading this post in the blog of Socket.io and found this: // 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 !! |
@dsalcedo |
If you want to prevent someone from connecting to your server you should use 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. |
@DannyvanderJagt |
I think I understand your problem now. 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) |
👍 |
One of first thing i tried... But not work corectly for me... |
...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 😄 ! |
Please close this question listed as 'issue'. For questions please use stackoverflow. |
How ban one user using socket.id in 1.0 series?
The text was updated successfully, but these errors were encountered: