WebRTC onicecandidate永远不会在Chrome中触发

时间:2019-05-26 07:16:33

标签: javascript google-chrome firefox webrtc

我的webrtc网络应用程序永远不会启用onicecandidate。 当我查看我的local(Webrtc Object)对象时,设置了localdescription和remotedescription。 在firefox中,它会触发onicecandidate,但event.candidate为null

我试图解决这个问题了几个小时,但我不能

有人可以解决这个问题吗?

如何使onicecandidate事件被触发?

  let local = new RTCPeerConnection();
  let remote = new RTCPeerConnection();
  try {
    local.createOffer().then(function(sdp) {
      console.log("Offer Created by Local: " + sdp.sdp);
      local.setLocalDescription(sdp);
      remote.setRemoteDescription(sdp);

      remote.createAnswer().then(function(sdp){
        console.log("Answer Created by Remote: " + sdp.sdp);
        remote.setLocalDescription(sdp);
        local.setRemoteDescription(sdp);
        local.onicecandidate = localicecandidate;
        remote.onicecandidate = remoteicecandidate;

        let channel = local.createDataChannel("channel");
        channel.onopen = function(event) {
          console.log("Channel opened");
        }
        channel.onmessgae = function(event) {
          console.log(event.data);
        }

        remote.ondatachannel = function(event) {
          let channel = event.channel;
          channel.onopen = function(event) {
            channel.send("Hi!");
          }
        }

        function localicecandidate (event) {
          console.log("Local got new Ice Candidate: " + event.candidate);
          remote.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
        function remoteicecandidate (event) {
          console.log("Remote got new Ice Candidate: " + event);
          local.addIceCandidate(new RTCIceCandidate(event.candidate));
        }

      }); 
    })
  }

  catch (e) {
    console.log("Got Error" + e);
  }

1 个答案:

答案 0 :(得分:1)

您不会在调用createOffer之前调用createDataChannel,因此SDP不包含m =行。没有m线,ICE就无法谈判。

在createOffer之前移动value