[pion]测试你的TURN服务器

本文提供了一个简单的HTML页面用于测试TURN服务器是否可达。通过创建PeerConnection并指定ICE Server,即目标TURN服务器,然后触发ICE过程来实现。当onicecandidate回调中收到的候选类型为relay时,表明TURN服务器工作正常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

把下面的html用浏览器打开,填入你想要测试的turn服务器

<html>

<header>
    <title>ice测试</title>
</header>

<body>

<label for="serverAddress"></label>
<input id="serverAddress" value="turn:127.0.0.1:13902"/>
<br/>

<label for="username"></label><input id="username" value="foo"/>
<br/>

<label for="password"></label><input id="password" value="bar"/>
<br/>

<button onclick="window.test()">Test</button>

<script>
    function checkTURNServer(turnConfig, timeout) {

        return new Promise(function (resolve, reject) {

            let promiseResolved;
            setTimeout(function () {
                if (promiseResolved) return;
                resolve(false);
                promiseResolved = true;
            }, timeout || 5000);

            promiseResolved = false;

            let myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection   //compatibility for firefox and chrome
                , pc = new myPeerConnection({iceServers: [turnConfig]})
                , noop = function () {
            };

            pc.createDataChannel("");    //create a bogus data channel
            pc.createOffer({}).then((offer) => {
                if (offer.sdp.indexOf('typ relay') > -1) { // sometimes sdp contains the ice candidates...
                    promiseResolved = true;
                    resolve(true);
                }
                pc.setLocalDescription(offer)
            })

            pc.onicecandidate = function (ice) {  //listen for candidate events
                if (promiseResolved || !ice || !ice.candidate
                    || !ice.candidate.candidate
                    || !(ice.candidate.candidate.indexOf('typ relay') > -1)) return;

                console.log("ice candidate=", ice.candidate)

                // If a relay candidate was found, notify that the TURN server works!
                if (ice.candidate.type === "relay") {
                    console.log("The TURN server is reachable !");
                }

                promiseResolved = true;
                resolve(true);
            };
        });
    }

    window.test = () => {
        let serverAddress = document.getElementById("serverAddress").value;
        let username = document.getElementById("username").value;
        let password = document.getElementById("password").value;

        console.log("Trigger ice test. Server address=", serverAddress,
            ", username=", username, ", password=", password)

        checkTURNServer({
            urls: serverAddress,
            username: username,
            credential: password
        }).then(function (bool) {
            console.log('is TURN server active? ', bool ? 'yes' : 'no');
        }).catch(console.error.bind(console));
    }

</script>
</body>
</html>

简单说来,就是创建一个PeerConnection,指定ICEServer,也就是我们的目标测试ICE。

传入本地的sdp,触发ice过程。

onicecandidate的回调中,会收到ice的结果,如果类型是relay,则表示turn服务器正常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值