You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS脚本测试coTURN服务器失败,但本地turnutils_uclient测试正常求助

问题:JS脚本无法检测到coTURN服务器可达性

我的coTURN服务器部署在host容器中,IP为10.200.197.74;Web服务器部署在桥接模式的WSL容器中,IP为10.200.194.65,已映射80和3478端口。两者可互相ping通,且从Web服务器执行turnutils_uclient 10.200.197.74 -u username -w password -y测试coTURN完全正常。但使用JS测试脚本时,页面始终显示“🔴 The TURN server is NOT reachable!”。

以下是我的JS测试代码和coTURN配置文件,希望解决这个问题:

JS测试代码

<!DOCTYPE html>
<html>

<head>
    <title>Test Stun/Turn Servers</title>

    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>

<body>
    <h1>Test Ice Servers</h1>

    <hr />

    <pre id='ice' style='overflow: auto'></pre>

    <hr />

    <p id='ip'></p>
    <p id='stun'>🔴 The STUN server is NOT reachable!</p>
    <p id='turn'>🔴 The TURN server is NOT reachable!</p>
    <p id='err'></p>

    <hr />

    <script>
        const Ice = document.getElementById('ice');
        const IP = document.getElementById('ip');
        const Stun = document.getElementById('stun');
        const Turn = document.getElementById('turn');
        const Err = document.getElementById('err');

        const iceServers = [
            // Test some STUN server
            {
                urls: 'stun:stun.l.google.com:19302',
            },
            // Test some TURN server
            {
                urls: 'turn:10.200.197.74',
                username: 'username',
                credential: 'password',
            },
        ];

        // Print iceServers config
        Ice.innerHTML = JSON.stringify(iceServers, null, 4);

        // Test the connections
        const pc = new RTCPeerConnection({
            iceServers
        });

        pc.onicecandidate = (e) => {
            if (!e.candidate) return;

            console.log(e.candidate.candidate);

            // If a srflx candidate was found, notify that the STUN server works!
            if (e.candidate.type == 'srflx' || e.candidate.candidate.includes('srflx')) {
                let ip = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/;
                let address = e.candidate.address 
                    ? e.candidate.address 
                    : e.candidate.candidate.match(ip);
                IP.innerHTML = '🟢 Your Public IP Address is ' + address;
                Stun.innerHTML = '🟢 The STUN server is reachable!';
            }

            // If a relay candidate was found, notify that the TURN server works!
            if (e.candidate.type == 'relay' || e.candidate.candidate.includes('relay')) {
                Turn.innerHTML = '🟢 The TURN server is reachable!';
            }
        };

        // handle error
        pc.onicecandidateerror = (e) => {
            console.error(e);
            Err.innerHTML = '🔴 Error: ' + e.errorText;
        };

        pc.createDataChannel('test');
        pc.createOffer().then(offer => pc.setLocalDescription(offer));
    </script>
</body>

</html>

coTURN配置文件

listening-ip=10.214.197.74

listening-port=3478
min-port=49152
max-port=49999

verbose
lt-cred-mech
user=username:password
解决方法
  • 修正coTURN配置IP错误:配置文件中listening-ip写的是10.214.197.74,但实际服务器IP为10.200.197.74,这会导致TURN监听错误IP。修改为listening-ip=10.200.197.74后重启服务。
  • 添加relay-ip参数:在配置文件中新增relay-ip=10.200.197.74,明确指定中继IP,避免自动选IP时出现异常。
  • 显式指定TURN端口:将JS脚本中的TURN地址改为urls: 'turn:10.200.197.74:3478',确保浏览器使用正确端口连接。
  • 完善ICE候选检测逻辑:原脚本仅在onicecandidate事件中检测relay候选,部分场景下relay候选会在ICE收集完成后生成,可添加以下代码补充检测:
pc.onicegatheringstatechange = () => {
    if (pc.iceGatheringState === 'complete') {
        pc.getStats().then(stats => {
            for (const stat of stats.values()) {
                if (stat.type === 'candidate-pair' && stat.remoteCandidateType === 'relay') {
                    Turn.innerHTML = '🟢 The TURN server is reachable!';
                    return;
                }
            }
        });
    }
};
  • 规避浏览器安全限制:不要通过file://协议打开测试页面,必须通过Web服务器的80端口访问(即http://10.200.194.65),WebRTC在本地文件协议下会有连接限制。

内容的提问来源于stack exchange,提问作者bropde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 04:11:05