Webrtc如何使onicecandidate在我的代码上工作

时间:2019-03-06 20:58:39

标签: javascript jquery webrtc

大家好,我在这个webrtc和javascript中非常陌生,我尝试完成一个视频流,但是在我的代码中从未调用过onicecandidate可以帮助我在哪里出错

 $('body #call').on('click', function () {
            let receiverid = $(this).attr('data-ids');
            const peerConnection = new RTCPeerConnection(configuration);
            navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
                localStream = stream;
                localStream.getTracks().forEach((track) => {
                    peerConnection.addTrack(track, localStream);
                    localVideo.srcObject = localStream;
                });

            }).catch(err => console.log(err));
            peerConnection.createOffer()
                .then(sdp => peerConnection.setLocalDescription(sdp))
                .then(function () {
                    console.log('is emitedd offer');
                    socket.emit('onOffer', { callerid: userid, receiverid, offer: peerConnection.localDescription });
                });
            peerConnection.ontrack  = event => handleRemoteStreamAdded(event.streams[0], userid);

            peerConnection.onicecandidate = function(event) {
                if (event.candidate) {
                    console.log('emit candidate');
                  socket.emit('candidate', id, event.candidate);
                }
              };
        });
    });

非常感谢

1 个答案:

答案 0 :(得分:1)

好了,我看到了一个更好的例子,我的错误是我从我的视频对象peerConnection.createOffer()中叫navigator.mediaDevices.getUserMedia({ video: true }).then(stream => { // create offer here });到场外