socket.io is not working with simple-peer

22
May 15, 2019, at 8:30 PM

i am willing to build one-to-one video chatroom with Socket.io & Simple-Peer and for one-to-one algorithm i used socket.io rooms. but it is not working with that so i got confused. before this, i had an algorithm which worked without rooms but it worked for only 2 persons and if third one tried to connect s/he was getting "waiting" message. in my current algorithm, room's "name" is number. if there is only one user in the room it is emitting special event and when second one connects room's "name" is incremented and third user gets to "2" room and .... here is my code:

Server side:

vchns.on('connection', async function(socket){
try{
  let room = vnumb;
  socket.join(room);
  socket.current_vroom = room
  vchns.in(room).clients((err,clients)=>{
    if(err) console.log(err);
    if(clients.length < 2){
      if(clients.length == 1){
      socket.emit('CreatePeer')
      }
    }
    if(clients.length >= 2){
      ++vnumb
    }
  })

  socket.on('Offer', SendOffer);
  socket.on('Answer', SendAnswer);

  function SendOffer(offer){
    socket.broadcast.in(socket.current_vroom).emit('BackOffer', offer);
  }
  function SendAnswer(data){
    socket.broadcast.in(socket.current_vroom).emit('BackAnswer', data);
  }
}catch(err){
  console.log(err.message)
 }
})

Client side:

let client = {}
navigator.mediaDevices.getUserMedia({video: true, audio: true})
.then(stream=>{
localVideo.srcObject = stream;
localVideo.play();
function initPeer(type){
 let peer = new Peer({initiator: (type == 'init')? true : false, stream: stream, trickle: false});
peer.on('stream', (stream)=>{
  CreateVideo(stream)
})
return peer;
}
function MakePeer(){
   client.gotAnswer = false;
   let peer = initPeer('init');
   peer.on('signal', function(data){
     if(!client.gotAnswer){
         socket.emit('Offer', data)
     }
 })
 client.peer = peer
}
  function FrontAnswer(offer){
    let peer = initPeer('notInit');
    peer.on('signal', (data)=>{
    socket.emit('Answer', data);
  })
 peer.signal(offer)
}
   function SignalAnswer(answer){
     client.gotAnswer = true;
     let peer = client.peer;
     peer.signal(answer)
   }
   function CreateVideo(stream){
     let video = document.createElement('video');
     video.id = 'remoteVideo';
     video.srcObject = stream;
     document.querySelector('#remoteVideoDiv').appendChild(video);
     video.play()
   }
  socket.on('BackOffer', FrontAnswer)
  socket.on('BackAnswer', SignalAnswer);
  socket.on('CreatePeer', MakePeer)
})
.catch(err=>{
console.log(err.message)
})

so what is wrong?

Thanks!

READ ALSO
ORM for oracle DB in nodejs

ORM for oracle DB in nodejs

could you please suggest me better ORM for Oracle DB ?

39
Get loopback models using loopback-datasource-juggler

Get loopback models using loopback-datasource-juggler

Is there any other way other than getting the loopback model by using app object in a custom js file?

15
How to test mongoose.connect callback method with Jest

How to test mongoose.connect callback method with Jest

I've started making a new Express application, and the testing framework of choice is Jest

43
Why isn&#39;t my peer-js chatroom system working?

Why isn't my peer-js chatroom system working?

I've got a chatroom system where a user can create a chatroom and then talk ONLY in voice chat, however, now, it calls my friend, but I can't hear them and the audio element doesn't get createdHere is my console output

14