I have this code in the server side to send to the client which users disconnect from the system:
data = [usuariosConectados,socket.nickname];
io.sockets.emit('usuarioDesconectado',data);
In the client side I have this:
websocket.on("usuarioDesconectado",procesarUsuarios);
function procesarUsuarios (mensaje)
{
console.log(mensaje);
$('#users').html('');
for(u=0; u<mensaje.length; u++)
{
for(var user in mensaje[u])
{
$('#users').append($('<p>').text(mensaje[u][user]));
}
}
}
The console.log(mensaje) shows this:
[{hola:"hola", lol:"lol"}, "Oscar"]
With this I know that the user Oscar is out of the system and hola and lol are still in.
The problem is in this part:
$('#users').append($('<p>').text(mensaje[u][user]));
this code shows the connected users but also the disconnected user like
O
s
c
a
r
How can I get the connected users separated from the disconnected?
i found my solution:
$('#users').html('');
if (mensaje[1] != null)
{
$('#chatInsite').append($('<p>').html('<span>'+ '<strong>' + mensaje[1] + " desconectado</strong></span> "));
for(i in mensaje[0])
{
$('#users').append($('<p>').text(mensaje[0][i]));
}
$('#chat').animate({scrollTop: $('#chatInsite').height()},800);
}
else
{
for(i in mensaje[0])
{
$('#users').append($('<p>').text(mensaje[0][i]));
}
console.log("Usuario Nulo");
}