How ban one user using socket.id in 1.0 series?
I think you need:
Any way, this is not a issue can you solve this 'question' in http://stackoverflow.com/
Code strong!
@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?
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!
@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.
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 !!
@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.
you're absolutely right, I've made changes and I disconnect the socket by server side
// 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
});
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!_
@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
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. https://github.com/socketio/socket.io/issues/2147#issuecomment-126320131_
:+1:
@DannyvanderJagt
@dsalcedo
One of first thing i tried... But not work corectly for me...
has same issue of this user https://github.com/socketio/socket.io/issues/47#issuecomment-17094329
...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 :smile: !
Please close this question listed as 'issue'. For questions please use stackoverflow.
Code strong.
Most helpful comment
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 insocket.io
1.0. See this migration guideYou can use it like this:
If you want to disconnect a client which is already connected you simple use:
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!_