gpt4 book ai didi

webrtc - STUN/TURN 服务器连通性测试

转载 作者:行者123 更新时间:2023-12-03 08:39:54 25 4
gpt4 key购买 nike

我想弄清楚如何测试 STUN/TURN 服务器是否处于事件状态并正确响应连接。理想情况下,此测试将从外部机器执行,以防万一 STUN/TURN 机器在这种情况下关闭,连接测试也应报告。

过去有人调查过这个案子吗?会推荐哪些解决方案?

最佳答案

编辑: github.io 中的一个很好的实现从评论到另一个答案(在 IceTransports 值中选择“中继”):

Test TURN Server

按照 Benjamin Trent 的建议,我编写了以下代码来测试 TURN 服务器的连接性,适用于 Firefox n chrome:

function checkTURNServer(turnConfig, timeout){ 

return new Promise(function(resolve, reject){

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

var promiseResolved = false
, 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(function(sdp){
if(sdp.sdp.indexOf('typ relay') > -1){ // sometimes sdp contains the ice candidates...
promiseResolved = true;
resolve(true);
}
pc.setLocalDescription(sdp, noop, noop);
}, noop); // create offer and set local description
pc.onicecandidate = function(ice){ //listen for candidate events
if(promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf('typ relay')>-1)) return;
promiseResolved = true;
resolve(true);
};
});
}

用法示例:

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

您可以运行以下代码段进行检查:

var res = id('result');

id('button').onclick = function(){
res.innerHTML = 'Checking TURN Server...';
var url = 'turn:'+id('url').value+':'+id('port').value,
useUDP = id('udp').checked;
url +='?transport=' + (useUDP ? 'udp': 'tcp');
checkTURNServer({
urls: url,
username: id('name').value,
credential: id('pass').value
}, id('time').value).then(function(bool){
if(bool)
res.innerHTML = 'Yep, the TURN server works...';
else
throw new Error('Doesn\'t work');
}).catch(function(e){
console.log(e);
res.innerHTML = 'TURN server does not work.';
});
};


function checkTURNServer(turnConfig, timeout){
console.log('turnConfig: ', turnConfig);
return new Promise(function(resolve, reject){

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

var promiseResolved = false
, 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(function(sdp){
if(sdp.sdp.indexOf('typ relay') > -1){ // sometimes sdp contains the ice candidates...
promiseResolved = true;
resolve(true);
}
pc.setLocalDescription(sdp, noop, noop);
}, noop); // create offer and set local description
pc.onicecandidate = function(ice){ //listen for candidate events
if(promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf('typ relay')>-1)) return;
promiseResolved = true;
resolve(true);
};
});
}


function id(val){
return document.getElementById(val);
}
#url{
width: 250px;
}
#port{
width: 70px;
}
<h1>
Test TURN server
</h1>
<div>
TURN URL: <input id='url' placeholder='example.com or xxx.yyy.rrr.ddd' />
Port: <input type='number' value='3478' id='port' placeholder='enter a port number' />
</div>
<div>
Transport: <input type="radio" name="transport" id="tcp" value="tcp" /> TCP
<input type="radio" name="transport" id="udp" value="udp" checked/>UDP
</div>

<div>
Username: <input id="name" placeholder="turn username" />
</div>
<div>
password: <input id="pass" placeholder="turn password" />
</div>

<div>
checking Timeout: <input type='number' id="time" placeholder="wait time before checking timeout" value=5000 />
</div>
<div>
<button id='button'>
Check TURN Server
</button>
</div>

<h4 id='result'></h4>

关于webrtc - STUN/TURN 服务器连通性测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28772212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com