Thursday 23 April 2020

RTC connection only works after second offer/answer?

Hello, I'm trying to get more familiar with Node, WebRTC, and socket.io, so I followed a tutorial on a quick video chat web app.I'll spare you the verbose code, but this diagram is how it's structured. Really put my heart into it so go easy on meThe issue is, when a "call" (function call to callUser(socketId)) is made from one client to another, it doesn't work the first time. As soon as a second "call" is made, the video feed displays as intended. Here is the code responsible for this functionality.Client:/* React to a call */ socket.on("call-made", async data => { await peerConnection.setRemoteDescription( new RTCSessionDescription(data.offer) ); const answer = await peerConnection.createAnswer(); await peerConnection.setLocalDescription(new RTCSessionDescription(answer)); console.log("Received call from " + data.from); // debug console.log("Sending answer...") socket.emit("make-answer", { answer, to: data.from }); }); /* React to an answer */ socket.on("answer-made", async data => { await peerConnection.setRemoteDescription( new RTCSessionDescription(data.answer) ); console.log("Received answer."); // debug }); async function callUser(socketId) { const offer = await peerConnection.createOffer(); await peerConnection.setLocalDescription(new RTCSessionDescription(offer)); console.log("Calling user: " + socketId); // debug socket.emit("call-user", { offer, to: socketId }); } Server:/* Call user */ socket.on("call-user", data => { socket.to(data.to).emit("call-made", { offer: data.offer, from: socket.id }); }); /* Answer user */ socket.on("make-answer", data => { socket.to(data.to).emit("answer-made", { from: socket.id, answer: data.answer }); }); Can anyone give some insight as to why this may be? Is it something I'm doing wrong, an error on my network, or just a fact of life that I'm not aware of? Thanks!

Submitted April 24, 2020 at 12:14AM by zDev19

No comments:

Post a Comment