如何在WebRTC中等待候选人



有什么简单的方法可以让await完全完成候选项吗?我们可以await报盘&描述,但我看不出有什么方法可以等待候选者(将完整的sdp发送到信令服务器)。

例如:

const pc = new RTCPeerConnection(configuration);
const sendChannel = pc.createDataChannel("sendChannel");
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
// need something like this;
await pc.onIceCandidateComplete();; 
// send the offer to signaling server once we have all the data.
send(offer);

我看到有oniceconnectionstatechange()onicecandidate()可以使用,但没有一个支持await async,或者我找不到一个好的例子。

有人为此想出了一个好的解决方案吗?或者有什么理由不具备这个功能吗?最好的方法是什么?

没有可直接放弃的事件。您可以使用类似waitUntilEvent函数的构造,如下所示:

await (new Promise(r => {
pc.addEventListener(
'icegatheringstatechange',
() => pc.gatheringState === 'complete' && r()
)
}));

然而,请注意,在某些情况下,这种状态变化可能需要花费大量时间(30s以上)。这个bug有一些注释。通常,您需要等待其他条件,如第一个中继候选者或添加超时。

最新更新