【问题标题】:WebRTC onicecandidate NEVER fires in chromeWebRTC onicecandidate 永远不会在 chrome 中触发
【发布时间】:2019-05-26 07:16:32
【问题描述】:

我的 webrtc 网络应用程序永远不会满足 onicecandidate。 当我查看我的本地(Webrtc 对象)对象时,设置了本地描述和远程描述。 在 Firefox 中,它会触发 onicecandidate 但 event.candidate 为空

我尝试了几个小时来解决这个问题,但我无法解决

谁能解决这个问题?

如何让 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);
  }

【问题讨论】:

  • onmessgae -- 你有一个错字。

标签: javascript google-chrome firefox webrtc


【解决方案1】:

您没有在调用 createOffer 之前调用 createDataChannel,因此您的 SDP 不包含 m=lines。没有 m 线,ICE 就没有什么可以协商的了。

在 createOffer 之前移动 let channel = local.createDataChannel("channel");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2015-02-06
    • 2021-05-22
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多