无法建立RTC对等连接

如何解决无法建立RTC对等连接

我了解了MDN中的WebRTC,并尝试打开对等连接。我决定在一个页面中同时打开本地和远程连接,并编写了以下代码

const configuration = {iceServers: [
    {urls: 'stun:stun.l.google.com:19302'},{urls: 'stun:stun1.l.google.com:19302'},]};

const localConnection = new RTCPeerConnection(configuration);
const remoteConnection = new RTCPeerConnection(configuration);
let localSendChannel,remoteSendChannel;

localConnection.onicecandidate = ({candidate}) => {
    console.log('local candidate',candidate);
    if (candidate) remoteConnection.addIceCandidate(candidate)
}

remoteConnection.onicecandidate = ({candidate}) => {
    console.log('remote candidate',candidate);
    if (candidate) localConnection.addIceCandidate(candidate)
}

const connect = async () => {
    const offer = await localConnection.createOffer();
    console.log('local offer',offer);
    await localConnection.setLocalDescription(offer);
    console.log('local localDescription',localConnection.localDescription);
    await remoteConnection.setRemoteDescription(localConnection.localDescription);
    
    const answer = await remoteConnection.createAnswer();
    await remoteConnection.setLocalDescription(answer);
    console.log('remote answer',answer);
    console.log('remote localDescription',remoteConnection.localDescription);
    await localConnection.setRemoteDescription(remoteConnection.localDescription);

    localConnection.addEventListener('connectionstatechange',(e) => {
        console.log('localConnection new state',e.connectionState);
    });
    
    remoteConnection.addEventListener('connectionstatechange',(e) => {
        console.log('remoteConnection new state',e.connectionState);
    });

    const gumStream = await navigator.mediaDevices.getUserMedia({video: false,audio: true});
    for (const track of gumStream.getTracks()) 
        localConnection.addTrack(track);
}

const openLocalChannel = async () => {
    localSendChannel = localConnection.createDataChannel("sendChannel");
    localSendChannel.onopen = () => {
        console.log('local datachannel was opened');
        localSendChannel.send("Hello,world!")
    }
    
    localSendChannel.onclose = () => console.log('local datachannel was closed');
    localSendChannel.onmessage = (msg) => console.log('local channel got message',msg);
}

const waitRemoteChannel = () => {
    remoteConnection.ondatachannel = (e) => {
        remoteSendChannel = e.channel;
        console.log('remote atachannel was init');
        
        remoteSendChannel.onopen = () => console.log('remote datachannel was opened');
        remoteSendChannel.onclose = () => console.log('remote datachannel was closed');
        remoteSendChannel.onmessage = (msg) => console.log('remote channel got message',msg);
    };
}

const start = async () => {
    await connect();
    waitRemoteChannel();
    await openLocalChannel();
    
    localConnection.addEventListener('connectionstatechange',async (e) => {
        console.log('localConnection new state',e.connectionState);
    });
}

start();

我在Chrome中没有任何候选人,而在FireFox中只有null个候选人。你能指出哪里错了吗?

已更新:我在连接创建后向代码媒体轨道添加添加和尝试创建数据通道的尝试。但是问题仍然存在

解决方法

您对连接不做任何事情,也没有添加轨道或创建数据通道。结果,该报价将在形式上有效,但不会导致冰候选人聚集,没有候选人,他们将没有任何联系。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?