Open vrajroham opened 7 years ago
actualy, I have fix that by
listening event user status change if status change is offline and user logout is initiator => you must select someone in you room and change user id to room id
there is still some problems https://github.com/muaz-khan/RTCMultiConnection/blob/master/Signaling-Server.js#L177 you need to fix it because listOfUsers[socket.userid] is null if initiator log out.
do you aware this case @muaz-khan
@karamata I'll try and let know does this works. Thanks
Same problem, here's the solution I came up with. It's working so far, but with large scale, I don't know.
Method:
Listen for when the primary user leaves, then change the userid of one user in the room to room_name. In my code below, the variable chat_room is set higher in the page (generated dynamically by php). Also, I'm using connection.getAllParticipants() and not listOfUsers[socket.userid] (not really sure of the difference between the two). I pulled some of this code from the documentation API then adjusted to suit.
Lots and lots of comments so you can follow my logic:
connection.onUserStatusChanged = function(event) {
if (event.status === 'offline') {
// Is the room owner here?
var remoteUserId = chat_room; // chat_room is the room name, set higher in the script.
var chatters = [connection.userid] // initialize array of users with me.
var isUserConnectedWithYou = connection.getAllParticipants().indexOf(remoteUserId) !== -1;
if (isUserConnectedWithYou) {
// The owner is here...
console.log('Primary user is here. Nothing to do.');
} else {
console.log('Primary user is not here!');
var numberOfUsersInTheRoom = connection.getAllParticipants().length;
console.log('users in room: ' + numberOfUsersInTheRoom); // does not include me!
if (numberOfUsersInTheRoom === 0) {
// I am the only one here. Take control.
connection.userid = chat_room;
connection.openOrJoin(chat_room); // Do I need to reconnect? I don't know... But this works...
console.log('I am the only one here. Take control. I am now the primary user.');
console.log('My userid is now: ' + connection.userid);
} else {
// There are multiple users here. Only one can take control.
// Fill our array.
connection.getAllParticipants().forEach(function(remoteUserId) {
chatters.push(remoteUserId);
});
// Sort our array.
chatters.sort();
// Whoever is at the top of the array gets control. Maybe it's me, maybe not.
if (connection.userid == chatters[0]) {
// Take control.
connection.userid = chat_room;
connection.openOrJoin(chat_room); // Do I need to reconnect? I don't know... But this works...
console.log('Ownership of room taken. I am now the primary user.');
} else {
console.log('I am not taking room control. User ' + chatters[0] + ' will take control.');
}
} // end if if (numberOfUsersInTheRoom === 0)
} // end if (isUserConnectedWithYou)
connection.onclose(event);
}
};
Any feedback is welcome. It's working great in my tests of 5-6 users. No one steps on anyone else. I don't know how it would work with 100+ users.
Also, when the former room owner rejoins, they see the new room owner and grab a random id... and everyone can interact fine.
CT
I found a small issue with my above method. In this scenario:
User A connects and is primary user. User B connects. User C connects. User A disconnects. User C assumes role of primary user. C changes connection.userid = room_name.
B and C can still talk, and if A reconnects, all 3 can talk.
However, B does not see the new userid for C. A sees it (on re-entry) and C sees it. But when I run:
connection.getAllParticipants().forEach(function(remoteUserId)
From B's point of view, there is no one in the room with the userid of room_name. B never 'sees' the action of C changing their connection.userid. B still sees C's original token userid.
It's not impacting anything, all 3 can still cross talk. But I discovered this when I wrote up some code to check the room and see if any user in it was the primary user. This may have other unforeseen implications.
Clear as mud?
CT
Ok, so I found an issue, relating to my last post.
After C assumes control, B does not see the change. The next time a user leaves, B will look for a primary user. C might still be in the room, but when B looks at C, B doesn't see C as the primary user... So B run's its userid sorting code and may try to assume control of the room.
If B sets its userid = room_name, and C is still in the room, B will cut itself off from everyone else.
So the fix here is when C takes control of the room, C needs to inform everyone else. If we can't push the new userid to everyone else, I will try setting some new extra info and see if that gets picked up by B. If it does, I can adjust the code to look at extra info for primary user and not userid = room_name.
Ok, so it appears that .extra info that is changed after a user enters a room, is not pushed to the other users in the room.
Does anyone know how to push updates a user makes to themselves across the channel? .userid and .extra...
If it can't be done I can send some invisible control codes across the channel and then have every user update their records for the new primary user.
Hi @CT-Thompson , were you able to resolve this somehow?
@CT-Thompson Hey, not sure if you are still onto this but you can get the data of the user and change it via connection.peers['user-id']
Here is for getting peers data: https://www.rtcmulticonnection.org/docs/peers/ Here is changing the userid: https://www.rtcmulticonnection.org/docs/changeUserId/
@duahimanshu100 I'll try to resolve this with the above code. I also need to solve it.
I am using Audio+Video+TextChat+FileSharing
If initiator leaves a session where there are other participants , then initiator cannot return, and in fact nobody else can join the session.
It seems that room get's closed and if Initiator returns then another room is created with same name and if any participant tries to refresh then it automatically connects to initiator.
There are multiple issues open in this context like #328, #10 etc.
@muaz-khan Any solutions?