I am trying to build a array in my socket io server and sending it to the client.
var roomList = io.sockets.manager.rooms;
// new Array to save the clients per room
var clientsPerRoom = new Array();
//for (var i = 0; i < roomList.length; i++) {
for (var room in roomList) {
room = room.replace("/", "");
// get the clients per room
var clients = io.sockets.clients(room).length;
// saving it in the array at the name of the room
clientsPerRoom[room] = clients;
}
// transmit it to the client
io.sockets.emit('roomList', roomList, clientsPerRoom);
On the client side
var clients = 1;
for (room in roomList) {
if (room.length > 0) {
room = room.replace("/", "");
clients = clientsPerRoom[room];
console.log("ROOM: '" + room + "' has '" + clients + "' CLIENTS");
}
}
At the client, "clientsPerRoom" is "[]" (empty?), and so "clients" is "undeined". What am I doing wrong?
At the console log from the server are the values 1 if a user is connectet. If more users are online then its still 1 but at least it should send this value to the client.
Thanks
Finaly I solved it:
var clients = io.sockets.clients(room).length;
// has to look like this:
//clientsPerRoom = {"test" : 3, "xyz" : 4};
clientString = '"' + room + '" : "' + clients + '",' + clientString;
console.log("clientsPerRoom[" + room + "] : " + clientsPerRoom[room]);
console.log("__END OF LOOP__");
}
}
//cut the "," et the end of the string (else: ERROR!)
clientString = clientString.substr(0, clientString.length-1);
// parse it with JSON to pretend beeing a object
clientsPerRoom = JSON.parse('{' + clientString + '}');
// now send it to the client
io.sockets.emit('roomList', roomList, clientsPerRoom, totalClients);
Now another problem is, that
var clients = io.sockets.clients(room).length;
is always 1...
Even solved yet.
Problem was: io.sockets.clients(room) only counts users who accepted webcam access, otherwise the are not "really" in the room yet.
Greetings