javascript - Only works on one-to-one of which was to be many-to-many, webrtc -


i developing conference style application (many-to-many) video calls this style. code available on github not have node.js experience, hence decided create own server using php.

i created server using websockets. simple - receives messages , forwards them other connected clients (i.e., not client sent message). - nothing more; nothing less.

but problem architecture not allow clients connect more 1 person, i.e., when client tries connect third person, additional streams fail. clients can connect one-to-one.

i not know whether error in javascript or if need improve server. can make connect clients join?

see code:

html

<script type="text/javascript" src="http://127.0.0.1/scr/js/jquery.js"></script> 

javascript

var server = new websocket('ws://127.0.0.1:1805/'),     mystream = null,     peerconn = null,     mediaconstraints = {         'mandatory': {             'offertoreceiveaudio': true,             'offertoreceivevideo': true         }     };   navigator.webkitgetusermedia({     audio: true,     video: true }, function(stream) {     mystream = stream;      $("body").append('<video width="320" height="240" muted="muted" autoplay="true" src="' + window.url.createobjecturl(stream) + '"></video>');      createpeerconnection();      peerconn.addstream(mystream);     peerconn.createoffer(function(sessiondescription) {         peerconn.setlocaldescription(sessiondescription);         console.log("sending offer description");         server.send(json.stringify(sessiondescription));     }, null, mediaconstraints); }, function() {     console.error('error in stream'); });  function createpeerconnection() {     console.log('creating peer connection');      peerconn = new webkitrtcpeerconnection({         'iceservers': [{             'url': 'stun:stun.l.google.com:19302'         }, {             'url': 'turn:107.150.19.220:3478',             'credential': 'turnserver',             'username': 'subrosa'         }]     }, {         'optional': [{             'dtlssrtpkeyagreement': 'true'         }]     });      peerconn.onicecandidate = function(event) {         if (event.candidate) {             server.send(json.stringify({                 type: 'candidate',                 label: event.candidate.sdpmlineindex,                 id: event.candidate.sdpmid,                 candidate: event.candidate.candidate             }));         } else {             console.error('candidate denied');         }     };     peerconn.onaddstream = function(event) {         console.log("adding remote strem");         $("body").append('<video width="320" height="240" autoplay="true" src="' + window.url.createobjecturl(event.stream) + '"></video>');     };     peerconn.onremovestream = function(event) {         console.log("removing remote stream");     }; } server.addeventlistener("message", function(message) {     var msg = json.parse(message.data);      if(!mystream) {         console.error('error in stream');     }      if (msg.type === 'offer') {         createpeerconnection();          console.log('adding local stream...');          peerconn.addstream(mystream);         peerconn.setremotedescription(new rtcsessiondescription(msg));          console.log("sending answer peer.");          peerconn.createanswer(function(sessiondescription) {             peerconn.setlocaldescription(sessiondescription);             server.send(json.stringify(sessiondescription));         }, null, mediaconstraints);     } else if (msg.type === 'answer') {         peerconn.setremotedescription(new rtcsessiondescription(msg));     } else if (msg.type === 'candidate') {         var candidate = new rtcicecandidate({             sdpmlineindex: msg.label,             candidate: msg.candidate         });         peerconn.addicecandidate(candidate);     } }, false); 

the problem you're trying use single peer connection, work single connected party. you'll have have additional peer connection each other party, , able associate websocket messages users , particular peer connection. yourself, or use library simplewebrtc manages multiple user sessions you.

edit:

a simplified explanation of how simplewebrtc works this, 1 option creating mesh network of connected clients (all clients connected each other client):

  1. a client joins "room"
  2. the client notified each client has joined room
  3. for each other client, client creates new peer connection , stores in array of connected peers
  4. when messages received on websocket, must associated id, used map proper peer connection

the critical difference in architecture yours creating single peer connection, need create, store, , track array of peer connections, , have map websocket messages particular peers.


Comments