WebRTC detect connection type

WebRTC detect connection type

Jun 25, 2021
Coding

Test on Chromium 87 and 90

Detecting the connection type of webrtc, p2p or relay.

// Listen for connectionstatechange on the local RTCPeerConnection
peerConnection.addEventListener('connectionstatechange', event => {
    console.info("connectionstatechange: ", event)
    console.info("peerConnection.connectionState: ", peerConnection.connectionState)
    if (peerConnection.connectionState === 'connected') {
        // Peers connected!
        console.info("peer connected")

        var senders = peerConnection.getSenders()
        senders.forEach( sender => {
            var iceTransport = sender.transport.iceTransport
            var pair = iceTransport.getSelectedCandidatePair();
            console.info("pair: ", pair)
            console.info("connect type, local: ", pair.local.type, ", remote: ", pair.remote.type)
        })

        //var iceTransport = peerConnection.getSenders()[0].transport.iceTransport;
        //iceTransport.onselectedcandidatepairchange = function(event) {
           // var pair = iceTransport.getSelectedCandidatePair();
            //console.info('pair ', pair)
            //console.info("local proto ", pair.local.protocol.toUpperCase())
            //console.info("remote proto ", pair.remote.protocol.toUpperCase())
        //}

    }else if(peerConnection.connectionState === 'failed'){
        console.info("peer connect failed, restartIce")
        peerConnection.restartIce()
    }
});