简体   繁体   中英

nodejs - Removing user completely on disconnect (socketio)

My socketio collecting users script;

var users = [];

io.sockets.on( 'connection', function( socket ) {    
socket.on('new user', function(data) {    
socket.nickname = data.nick;    
users[socket.nickname] = socket;    
});

});

I want to remove the disconnected user. Done this;

socket.on('disconnect', function() {
delete users[socket.nickname];
});

After that, there is no errors. But when I do

console.log(typeof users[socket.nickname]);

it gives me output of object

Also there are still some garbage data left about the user. (socket events listeners, namespace). What is the proper way to remove user completely without leaving any data ?

socket.on('disconnect', function() {
socket.get('nickname', function(err, user) {
  delete users[user];
  io.sockets.emit('update', users);
});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM