Open monthAndEclipse opened 3 years ago
Thanks. I was also facing the same problem. The myPeer.on("call") methods was also not getting triggered so I taught there was a problem with PeerJS.
This fixes the issue.
I believe the issue is a race condition between then the peer connection is ready and when the webcam/usermedia promise resolved.
Because of this, the other peers try to call the new users before they've subscribed to the on.call event.
A cheap solution is moving the entire user media promise chain into the on.open event.
myPeer.on('open', id => {
navigator.mediaDevices.getUserMedia({
video: true,
audio: true
}).then(stream => {
console.log("__Webcam Ready")
addVideoStream(myVideo, stream)
myPeer.on('call', call => {
console.log("INCOMING CALL")
call.answer(stream)
const video = document.createElement('video')
call.on('stream', userVideoStream => {
addVideoStream(video, userVideoStream)
})
})
socket.on('user-connected', userId => {
connectToNewUser(userId, stream)
})
console.log("__Peer Open")
socket.emit('join-room', ROOM_ID, id)
})
})
Thanks. I was facing the same problem, your Timeout
solution fixed it :)
socket.on('user-connected', userId => {
setTimeout(connectToNewUser,1000,userId,stream)
})
I believe the issue is a race condition between then the peer connection is ready and when the webcam/usermedia promise resolved.
Because of this, the other peers try to call the new users before they've subscribed to the on.call event.
A cheap solution is moving the entire user media promise chain into the on.open event.
myPeer.on('open', id => { navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(stream => { console.log("__Webcam Ready") addVideoStream(myVideo, stream) myPeer.on('call', call => { console.log("INCOMING CALL") call.answer(stream) const video = document.createElement('video') call.on('stream', userVideoStream => { addVideoStream(video, userVideoStream) }) }) socket.on('user-connected', userId => { connectToNewUser(userId, stream) }) console.log("__Peer Open") socket.emit('join-room', ROOM_ID, id) }) })
God
I believe the issue is a race condition between then the peer connection is ready and when the webcam/usermedia promise resolved.
Because of this, the other peers try to call the new users before they've subscribed to the on.call event.
A cheap solution is moving the entire user media promise chain into the on.open event.
myPeer.on('open', id => { navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(stream => { console.log("__Webcam Ready") addVideoStream(myVideo, stream) myPeer.on('call', call => { console.log("INCOMING CALL") call.answer(stream) const video = document.createElement('video') call.on('stream', userVideoStream => { addVideoStream(video, userVideoStream) }) }) socket.on('user-connected', userId => { connectToNewUser(userId, stream) }) console.log("__Peer Open") socket.emit('join-room', ROOM_ID, id) }) })
It's working with me, but create 1 video undefined
I run the code locally, use chrome to vist http://localhost:3000/ , and it redirect to the room address 'http://localhost:3000/fe3b88cb-8749-44a4-ab6e-aed79bd23fe4' . then I open another tab in incognito mode with the same room address, but I can only see one video element in each tab .
F12, open my devtool , and add breakpoint on this line (in both two tab) ,refresh one of the page.
the program stop at this line ,I resume. then there are two video element, it works!
I was thinking there migth be a race problem. the
connectToNewUser
get executed before themyPeer.on('call')
, so I add a setTimeout method.and this time , without adding the breakpoint , there are two video elements in each tab.
does anyone have any ideas about what's happening here or is there a better solution in this case? cause it seems setTimeout it's a bad practice .