Currently, I am testing a new feature in a chat application that includes displaying a user list for those who have joined the chat. The challenge is to change the color of a specific user's name on the list when they get disconnected or leave the chat. For example, if Andy and Rubin are in the chat, and Rubin leaves, only Rubin's name should change color while Andy's name remains the same. However, I am facing an issue where when a user disconnects, the color change applies to all members on the list instead of just the one who left.
server.js
socket.on('disconnect',function(data){
console.log('user disconnected');
if(!socket.nickname)
return;
socket.broadcast.emit('userDisconnect',{nick:socket.nickname});
})
chat.js
socket.on('userDisconnect',function(data){
// var str = [];
// str.push(data.nick)
// for(var i= 0;i<data.length;i++){
// str = data.nick[i]
// }
// $users.text(data.nick).css("color","red") //disconnected user is only reflected on the list,rest of them vanish
$users.val(data.nick).css("color","red")
})
Note: Various attempts were made as outlined in the commented sections of the code.
My expectation was for only the username of the user leaving to change color in the list, but currently, when any user leaves, the color adjustment affects all users on the list.