So, what I am doing right now is creating a new client object as new connections are received, and storing them within a self managed object; on user disconnect, I am removing the object. This is the basis of what it looks like:
var socketobj = {};
io.sockets.on('connection', function(socket){
socketobj[socket.id] = socket;
socket.on('disconnect', function(data){
delete socketobj[socket.id];
});
});
I am using each of these client objects to store extra information, such as associated usernames etc. in the form of socketobj[socket.id].user = username;
The issue I am having is that a separate portion of my code will be running, and if the client disconnects just prior to the server accessing their client object, it will come up as undefined (because the object has just been deleted), and it will throw an exception.
How can I prevent this from happening? At the very minimum, how can I stop the errors from crashing my server everytime they are thrown?
Ok, I didn't know it was event based not multithreaded, here's a new answer.
Just check if the entry exists before accessing it. If it does not, it just got deleted and you can do nothing/a different thing as is appropriate.