I am making a chat, with user list.
I order to send user list on each user connexion i do the following action from the server :
io.on('connection', function (socket) {
var users = [];
socket.on('user connexion', function (username) {
if (typeof users[socket.handshake.address] == "undefined" ) {
users[socket.handshake.address] = [];
}
users[socket.handshake.address].push({username : username, color: "#"+((1<<24)*Math.random()|0).toString(16)});
console.log(users);
io.emit('user connexion', {userlist : users});
});
}
and in the front-end script i do :
socket.on('user connexion', function(userList){
console.log(userList);
});
But the console display :
Object {userlist: Array[0]}
Any ideas ?
Is your socket.handshake.address a numeric value?
I think you wanted users to be an Object and make a hash-map out of it by adding as socket.handshake.address property your data ({username : username, color: "#"+((1<<24)*Math.random()|0).toString(16)}).