当我试图从下面的代码返回localDescription时,我会得到一个在sdp下不包含candidate参数的响应,如屏幕截图所示。
有谁能帮我理解我在这里错过了什么吗?
另外,我并不是故意使用conn.onicecandidate = evt => {}这样的东西,因为我直接想从下面的函数返回一个值。
async function getLocalDesc() {
const conn = new RTCPeerConnection();
conn.createDataChannel('');
const offer = await conn.createOffer();
await conn.setLocalDescription(offer);
return conn.localDescription;
}
(async () => {
console.log(await getLocalDesc())
})();

发布于 2022-07-23 14:49:54
冰候选人聚会需要时间,而你只需等待setLocalDescription,它就开始了这个过程。它不需要等待收集完成或任何候选,您需要等待candidate事件或gatheringstatechange事件(到已完成的gatheringState )才能包含候选文件。
但是,https://webrtchacks.com/trickle-ice/有一些关于为什么这不是一个好主意的细节。
发布于 2022-07-23 15:28:01
我们可以从我们的函数中返回一个承诺,它将提供所需的param,即candidate,一旦它得到解决。
async function getLocalDesc(){
const conn = new RTCPeerConnection();
conn.createDataChannel('');
const offer = await conn.createOffer();
await conn.setLocalDescription(offer);
return new Promise(resolve => conn.onicecandidate = event => resolve(event));
}
(async () => {
console.log(await getLocalDesc())
})();
https://stackoverflow.com/questions/73090985
复制相似问题