javascript - WebRTC onicecandidate 永远不会在 Chrome 中触发

标签 javascript google-chrome firefox webrtc

我的 webrtc 网络应用程序从来没有出现过 onicecandidate。 当我查看本地(Webrtc 对象)对象时,设置了本地描述和远程描述。 在 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);
  }

最佳答案

在调用createOffer之前您没有调用createDataChannel,因此您的SDP不包含m=lines。如果没有 m 线,ICE 就没有任何谈判余地。

let channel = local.createDataChannel("channel"); 移至 createOffer 之前。

关于javascript - WebRTC onicecandidate 永远不会在 Chrome 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311461/

相关文章:

java - 判断客户端浏览器是否安装了java,是否可以启动小程序

javascript - 在 Firefox 上使用 popState 平滑滚动和返回按钮 - 需要点击两次

javascript - 在 FabricJS 中创建 "Tools"?

javascript - 使用 JavaScript 查找图像的高度 - 图像加载错误

javascript - 如何在动态创建的表格中添加样式

javascript - javascript函数什么时候不是对象?

google-chrome - Selenium Google 登录 block

android - 渐进式 Web 应用程序与原生应用程序在 Android 上具有哪些功能,反之亦然

javascript - 网页开发: how to show all errors (make errors verbose)?

html - 在谷歌浏览器中隐藏视频的 native 播放按钮