我有一个webrtc应用程序,它工作正常,但出于测试目的,我需要测试我的TURN服务器是否工作,但因为两个测试设备都在同一个网络中,我无法测试,认为下面的代码会限制候选人只有那些使用TURN服务器, 
function onIceCandidate(event, targetSessionID, targetUserName) {
    if (event.candidate) {
    var candidate = event.candidate.candidate;
    if(candidate.indexOf("relay")<0){ // if no relay address is found, assuming it means no TURN server
        return;
    }
    sendMessage(candidate); // using socket.io to send to the otherside
...
但是我注意到了(非常沮丧),这不起作用,因为当同伴创建答案描述时,
....
a=candidate:0 1 UDP 2128609535 13.198.98.221 58779 typ host
a=candidate:0 2 UDP 2128609534 13.198.98.221 58780 typ host
....
这意味着,通信是直接的,而不是通过TURN服务器,我假设这是正确的吗?现在,如何强制webrtc使用TURN服务器?
             
            
            
            
只是添加这个来关闭问题, 
function onIceCandidate(event, targetSessionID, targetUserName) {
    if (event.candidate) {
    var candidate = event.candidate.candidate;
    if(candidate.indexOf("relay")<0){ // if no relay address is found, assuming it means no TURN server
        return;
    }...
上面的代码工作,检查 wireshark, 
加入后 if(candidate.indexOf("relay")<0) 条件,通信只通过TURN服务器进行,如果服务器不存在/不正确的细节,连接状态得到了打击 new
编辑: 据卡伦在答案中所说的那样 w3 webrtc通过 relay 如 iceTransportPolicy 应该工作,但我还没有检查它是否在Firefox和Chrome中实现...
 
                
我不知道浏览器是否或什么时候支持这个但是看看draft-ietf-rtcweb-jsep-08的4.1.1节中的“ICE候选策略”,你可以看到如何将策略设置为“relay”会做你想做的。在当前的W3C API草案中,使用RTCIceTransports值“relay”为配置中的iceTranports字段设置。在中搜索RTCIceTransports https://w3c.github.io/webrtc-pc/
 
                
为了在firefox上进行测试,您可以强制使用TURN继电器。
检查我的答案 这里!