Search code examples
javascriptwebrtcstunturn

RTCPeerConnection.onicecandidate does not work


I was connected to stun turn server but onicecandidate doesnt working. I am running app from local file.

Script:

const pc = new RTCPeerConnection({
        iceServers:[{
            urls:"stun:stun.l.google.com:19302",
        },
        {
            url: 'turn:turn.bistri.com:80',
            credential: 'homeo',
            username: 'homeo'
        }]
    })
    const dc = pc.createDataChannel("channel")
    pc.onicecandidate = (e) => {
        console.log("ok")
    }
    dc.onopen = () => {
        console.log("Data Channel Opened")
    }
    dc.onmessage = (data) => {
        console.log("Message:"+data)
    }

Solution

  • onicecandidate event will occur when call pc.setLocalDescription.

    you didn't call setLocalDescription yet.