【问题标题】:RTC peer connection doesn't establishRTC 对等连接未建立
【发布时间】:2020-10-02 20:42:06
【问题描述】:

我在 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);
    });
    
    remoteConnection.addEventListener('connectionstatechange', (e) => {
        console.log('localConnection new state', e.connectionState);
    });
}

start();

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

更新:我在代码中添加了媒体轨道,在连接创建后添加和尝试创建数据通道。但问题是留下来

【问题讨论】:

    标签: webrtc simplewebrtc


    【解决方案1】:

    您没有对连接做任何事情,既没有添加轨道也没有创建数据通道。因此,offer 将正式有效,但不会导致 ice 候选人被收集,没有候选人将不会有任何联系。

    【讨论】:

    • 我试图在“connectionstatechange”处理程序中创建数据通道,但没有被调用。还是方式不对?
    • 我更新了问题中的代码。现在我添加曲目并创建数据通道
    • 现在您尝试在创建数据通道之前进行连接,因此您仍然遇到同样的问题。在频道创建后移动 connect() 调用
    • 成功了,谢谢!如何将您的评论标记为答案?
    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多