webrtcsimplewebrtc

RTC peer connection doesn't establish


I read about WebRTC in MDN and tried to open peer connection. I decided to open both local and remote connections in one page, and wrote this code:

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();

I haven't any candidates in Chrome and have only null candidates in FireFox. Can you point, where is mistake?

Updated: I added to the code media tracks adding and trying of create datachannel after connection create. But problem is staying


Solution

  • You are not doing anything with the connection, neither adding a track nor creating a datachannel. As a result the offer will be formally valid but will not cause ice candidates to be gathered and without candidates there will be no connection.