How to handle socket.io multiple connections on a webRTC many-to-many system?

落爺英雄遲暮 提交于 2019-12-24 16:35:27

问题


I have build a webrtc many-to-many system (a few months ago:WebRTC videoconferencing (many-to-many)), using socket.io as signaling channel. It works fine, but I am not handling yet many rooms for many users, I'm just putting all users together in one single room (an users array on the server-side, node.js). First of all, When I was building the system i realized that I couldn't call io.sockets.emit("room") for emitting when a new participant arrived and then creating offers and answers. The users (already in the room) couldn't send their offers to the new participant at the same time and the new participant couldn't send the answer to those users at the same time. It has to be a one-to-one connection process, so one user (already in the room) sends the offer to the new participant and this last one returns the answer and so on with each user. I made this by passing the new participant socket_id through the users, so when user number one fishishes the process connection with the new participant, he calls the server and sends new participant the socket_id, then the server calls (io.sockets.socket("target").emit(...)) user number two and so on. I want to know if i am doing right, that i cannot make the offer/answer negotiation between the users (already in room) and the new participant at the same time.

On the other hand, I know that socket.io handles multiplexing, and many sockets can connect at the same time, but if I am working with global variables to manage offer/answer negotiations, for example an index variable that increases as the user already in room got connected with the new participant, i mean this:

users[index];//let's say we have 5 users and currentuser is the new guy
io.sockets.socket(users[index]).emit('user incoming', currentuser);

and suddenly other new participant arrives while currentuser is still connecting to the rest of the users, let's say, user number 4, so index value would be 3 (programmatically). What would happen in this case?, because this sudden new participant would be the new currentuser and the previous currentuser has not finished connecting to all users, I mean, They are using the same variables, the same variable instance, I got confused at this point. I want to implement code for creating many rooms for many user but I need to understand this.

I have tested my app and everything works fine, but I have not tested with concurrent users connecting.

Should I work with the Socket.io functions (emit, clients, etc) instead of using global variables?.

Finally my code, focus on socket.on('user incoming', function (room){...}

I took this from Real Time communications with webRTC by Simon Pietro Romano, Sam Dutton is also a hard guy on this topic:

var users = [];
var currentuser;
var index = -1;

io.sockets.on('connection', function (socket){

    // Handle 'message' messages
    socket.on('message', function (message, target) {
            log('S --> got message: ', message);
            // channel-only broadcast...
            io.sockets.socket(target).emit('message', message, socket.id);
    });

    // Handle 'create or join' messages
    socket.on('create or join', function (room) {
            var numClients = io.sockets.clients(room).length;

            log('S --> Room ' + room + ' has ' + numClients + ' client(s)');
            log('S --> Request to create or join room', room);

            // First client joining...
            if (numClients == 0){
                    socket.join(room);
                    currentuser = socket.id;
                    //users.push(socket.id);
                    log('socked id:' + socket.id);
                    socket.emit('created', room);
            } else if (numClients > 0 && numClients < 6 ) {
            // Second client joining...                 
                    //io.sockets.in(room).emit('join', room);
                    socket.join(room);
                    currentuser = socket.id;
                    log('socked id:' + socket.id);
                    socket.emit('joined', room);                                            

            } else { // max two clients
                    socket.emit('full', room);
            }
    });

    // Handle new incoming user
    socket.on('user incoming', function (room) {

            index++;
            if(index < users.length){

                log('calling user ' + users[index]);                            
                io.sockets.socket(users[index]).emit('user incoming', 
                currentuser);

            }else{
                users.push(currentuser);
                index = -1;
            }

            /


    });

    socket.on('renegotiation', function (message, target) {
            log('S --> got message: ', message);
            // channel-only broadcast...
            io.sockets.socket(target).emit('renegotiation', message,  
            socket.id);
    });

    function log(){
        var array = [">>> "];
        for (var i = 0; i < arguments.length; i++) {
            array.push(arguments[i]);
        }
        socket.emit('log', array);
    }
});   

回答1:


The concept of a "room" is only a conceit to discover participants. Some libraries may use it, but it's not a concept in baseline WebRTC, thus not helpful in discussing WebRTC.

Unless you're routing through some central media-server, you probably have a mesh of one-to-one connections between each participant to all the other participants. I've seen this scale to 3 to 5 participants max.

Using a "room" model to set up and tear down connections, sounds entirely like application logic, and largely orthogonal to any WebRTC specific problems, so you might want to isolate the problem and ask a simpler question. Hope that helps.



来源:https://stackoverflow.com/questions/36882266/how-to-handle-socket-io-multiple-connections-on-a-webrtc-many-to-many-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!