Good afternoon, I have two array: usersConnected
and users
.
I am working with NodeJS
and Socketio
developing a chat.
To obtain the connected users I create a UID in LocalStorage same that I get through socketio.
In NodeJS I use two array called: usersConnected
and users
.
When a user accesses the app, he / she verifies if his / her UID is in both arrays if he / she is not found and keep it that way:
socket.on('users', function (user_uid) {
if ( usersConnected.indexOf(user_uid) < 0 ) {
usersConnected.push(user_uid);
}
if ( users.indexOf(user_uid) < 0 ) {
users.push(user_uid);
}
});
Afterwards if the user makes a refresh to the site or abandons it, I remove its UID from the usersConnected array and wait 5 seconds.
Passing 5 seconds if the user comes back he registers his UID in the usersConnected array but if he does not return then I verify that his UID is no longer in the usersConnected array and I remove it from the users array to verify who is really on the site . I do this like this:
socket.on('unload', function(user_uid){
usersConnected.splice( usersConnected.indexOf(user_uid), 1);
setTimeout(function () {
if ( usersConnected.indexOf(user_uid) < 0 ) {
var index = users.indexOf(user_uid);
users.splice(index, 1);
}
}, 5000);
});
It works well when I do it from a single browser, if I open another it registers well its UID then I have two UID something like this: [12345, 67890]
but if I update more than 3 times from any of the 2 browsers I do not know what happens that eliminates to the other UID's of the array leaving only its own UID.
Until now, I do not understand the reason why when updating 3 times the other UIDs are deleted, it seems that it overwrites the array users.
I hope you can help me.