Instead of trying to call any new users in the userList whenever it gets updated, it should be the responsibility of the user just joining the room to call everyone else. In this way you set up a chain:
- User X joins the room
- User Y joins the room
- User list is updated for user X
- User Y calls user X
- User Z joins the room:
- User list is updated for users X and Y
- User Z calls user X
- User Z calls user Y
And so on. The way to do this is by keeping track of when the user first enters the room.
When entering a room, the event listeners are executed in the following order:
- room entry listener
- room occupant listener
For other users who are already in the room, only the room occupant listener will be executed.
This means that when you enter a room, you get the current user list and then you can loop through and call each user. But the second time you get an updated user list, in the room occupant listener, you do not call any other user.
The solution for calling other users whenever the user list is updated is:
var needToCallOtherUsers;
easyrtc.setRoomEntryListener(function(entry, roomName) {
needToCallOtherUsers = true;
});
easyrtc.setRoomOccupantListener(function(roomName, userList, selfInfo) {
var easyrtcid;
if (needToCallOtherUsers) {
for (easyrtcid in userList) {
easyrtc.call(
easyrtcid,
function success(otherCaller, mediaType) {
console.log('success: ', otherCaller, mediaType);
},
function failure(errorCode, errorMessage) {
console.log('failure: ', errorCode, errorMessage);
}
);
}
needToCallOtherUsers = false;
}
});