如何处理webRTC多对多系统上的socket.io多个连接?

我已经build立了一个webrtc多对多系统(几个月前: WebRTCvideo会议(多对多) ),使用socket.io作为信令通道。 它工作正常,但我没有处理许多用户的许多房间,我只是把所有的用户放在一个单一的房间(服务器端的用户数组,node.js)。 首先,当我构build系统时,我意识到当新参与者到达时,我不能调用io.sockets.emit(“房间”),然后创build优惠和答案。 用户(已经在房间中)不能同时向新参与者发送报价,新参与者不能同时向这些用户发送答案。 它必须是一对一的连接过程,所以一个用户(已经在房间里)把这个提议发送给新的参与者,最后一个用户返回每个用户的答案。 我通过将新的参与者socket_id传递给用户来做到这一点,所以当第一个用户钓鱼与新参与者的进程连接时,他调用服务器并发送新的参与者socket_id,然后服务器调用(io.sockets.socket(“目标“)。发出(…))用户号码二等等。 我想知道我是否正确,我不能同时在用户(已经在房间里)和新参与者之间进行提供/回答协商。

另一方面,我知道socket.io处理多路复用,并且许多套接字可以同时连接,但是如果我使用全局variables来pipe理报价/响应协商,例如一个索引variables随着用户已经增加在房间里与新参与者有联系,我的意思是:

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

并突然其他新的参与者到达currentuser仍然连接到其余的用户,比方说,用户数4,所以索引值将3(以编程方式)。 在这种情况下会发生什么?因为这个突然的新参与者将成为新的当前用户,并且以前的当前用户还没有完成连接到所有用户,我的意思是,他们使用相同的variables,相同的variables实例,我对此感到困惑点。 我想要为许多用户创build许多房间的代码,但我需要了解这一点。

我testing了我的应用程序,一切工作正常,但我没有与并发用户连接testing。

我应该使用Socket.io函数(发出,客户等),而不是使用全局variables?

最后我的代码,关注socket.on('user incoming',function(room){…}

我从Simon Pietro Romano的webRTC实时通信中获得了这个信息,Sam Dutton也是这个话题中的一个硬汉:

 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); } }); 

“房间”的概念只是发现参与者的一个自负。 有些图书馆可能会使用它,但它不是WebRTC基本的概念,因此在讨论WebRTC时没有帮助。

除非您通过某个中央媒体服务器进行路由,否则您可能在每个参与者与所有其他参与者之间有一对一的连接。 我已经看到这个规模最多3到5个参与者。

使用“房间”模型build立和拆除连接,听起来完全像应用程序逻辑,并且基本上正交于任何WebRTC特定问题,所以您可能想要隔离问题并提出一个更简单的问题。 希望有所帮助。