Webrtc 在候选对象收集期间不会触发OniceAgheringStateChange事件

Webrtc 在候选对象收集期间不会触发OniceAgheringStateChange事件,webrtc,Webrtc,我正在编写一个webrtc应用程序,在这里我不能使用涓流冰。因此,我正在等待ICE候选人聚会完成并将报价发送给其他同行,以便ICE候选人被纳入SDP。我已经为onicegatheringstatechange设置了事件句柄,并等待iceGatheringState更改。但这一事件并未触发 pc = new RTCPeerConnection(peerConnectionConfig, peerConnectionConstraints); pc.onicegatheringstatechange

我正在编写一个webrtc应用程序,在这里我不能使用涓流冰。因此,我正在等待ICE候选人聚会完成并将报价发送给其他同行,以便ICE候选人被纳入SDP。我已经为onicegatheringstatechange设置了事件句柄,并等待iceGatheringState更改。但这一事件并未触发

pc = new RTCPeerConnection(peerConnectionConfig, peerConnectionConstraints);
pc.onicegatheringstatechange = onIceGatheringStateChange;

为了让它工作,我还需要做些什么吗?

不要等待调用OniceAgheringStateChange

这就是你应该做的:

  • 使用onicecandidate函数接收ice候选项
  • 当您收到“null”事件时,您知道已收到所有候选人
  • 如果没有通过计时器接收到“null”事件,请继续调用
  • 下面是一个粗略的代码示例(仅用作模板):

    使用带有计时器的onicecandidate事件非常重要,因为如果使用多个眩晕和转弯服务器,冰收集过程可能需要几秒钟,尤其是在接收到“null”事件之前。使用此技术,您甚至可以在继续调用之前等待特定数量的候选对象,因为您不需要所有候选对象为您的应用程序生成适当的SDP。还有一些方法可以通过在接收到的每个ice候选者之间启动一个非常小的计时器来改进这种方法


    在本例中,我设置了一个最大延迟为1000ms的计时器,因为我假设浏览器此时已接收到一个可接受的候选ice。这取决于你去测试,看看什么是最好的用户体验

    谢谢@syno。我是用你提到的方法做的。我被认为是为了这个目的而利用聚集国。
     var timer; // Some globally accessible timer variable
     var state = "not sent"; // Keep track of call state
    
     pc.onicecandidate = function(event) {
    
       if (!event.candidate) {
         // last candidate received. Check if SDP was already sent.
         if(state != "sent"){
            clearTimeout(timer);
            // Send SDP to remote peer:
            // Send pc.localDescription
            // Change call state to "sent"
         }
       }else{
         // Start a timer for the max "wait" time for ice candidates.
         timer = setTimeout(function(){
            // Ice gathering too slow, send SDP anyway.
            // Send pc.localDescription
            // Change call state to "sent"
         }, 1000);
       }
     }