当我创建一个 new RTCPeerConnection()
时,我无法用 peer.close() 销毁它,据我所知,一段时间后我用完了资源。当网页创建大量 RTCPeerConnection
时,它会在大约 600 次尝试后停止工作并在 Chrome 中抛出以下错误:
Uncaught DOMException: Failed to construct 'RTCPeerConnection': Cannot create so many PeerConnections
这是一个测试脚本,可以帮助您找出错误。这仅是说明性的,即使创建对等点的速度慢得多,也会出现问题:
var i = 1;
function peer() {
var peer = new RTCPeerConnection();
setTimeout(() => {
peer.close();
peer=null;
}, 10);
console.log(i++);
}
setInterval(peer, 20);
// run the script and wait on console to see the error
我希望它在调用 peer.close() 时释放所有资源,或者至少在 peer 设置为 null 时释放所有资源,所以我看不出为什么浏览器会抛出有这么多 PeerConnections 的错误。我做错了什么吗?我如何正确销毁同行?谢谢
最佳答案
尝试将 peer 设置为 null 以便它被垃圾收集。参见 these notes来自实现限制的人。如果这不起作用,请在该邮件列表线程中询问
关于javascript - 如何在 JavaScript 中正确销毁 RTCPeerConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058329/