简单对等 WebRTC 错误:Ice 连接失败
Posted
技术标签:
【中文标题】简单对等 WebRTC 错误:Ice 连接失败【英文标题】:Simple-peer WebRTC Error: Ice connection failed 【发布时间】:2018-04-15 15:08:30 【问题描述】:我无法让 Simple-Peer NPM 库在浏览器中连接两个不同网络中的两台计算机。如果计算机在同一个本地网络中,则浏览器相互连接,否则,我无法使它们连接。
两个浏览器都返回这个错误:
"Error: Ice connection failed.
at Peer._onIceStateChange"
at RTCPeerConnection.Peer.self._pc.oniceconnectionstatechange
信号看起来很适合我:
type: 'offer',
sdp: 'v=0\r\no=- 2275520429720515716 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE data\r\na=msid-semantic: WMS\r\nm=application 49523 DTLS/SCTP 5000\r\nc=IN IP4 5.12.206.160\r\na=candidate:2020300070 1 udp 2113937151 192.168.2.8 49523 typ host generation 0 network-cost 50\r\na=candidate:842163049 1 udp 1677729535 5.12.206.160 49523 typ srflx raddr 192.168.2.8 rport 49523 generation 0 network-cost 50\r\na=ice-ufrag:Ph/x\r\na=ice-pwd:csnTbzHs+dxzakEKPY8LfvBg\r\na=fingerprint:sha-256 B9:C0:9D:91:46:1B:E8:5C:83:B1:11:A7:C5:D7:64:97:A6:63:D9:12:11:0F:9A:05:8F:46:83:BC:90:38:86:9E\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n'
type: 'answer',
sdp: 'v=0\r\no=- 1356997482353729498 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE data\r\na=msid-semantic: WMS\r\nm=application 56005 DTLS/SCTP 5000\r\nc=IN IP4 86.126.104.54\r\nb=AS:30\r\na=candidate:2702239670 1 udp 2113937151 192.168.1.103 56003 typ host generation 0 network-cost 50\r\na=candidate:842163049 1 udp 1677729535 86.126.104.54 56005 typ srflx raddr 192.168.1.103 rport 56003 generation 0 network-cost 50\r\na=ice-ufrag:W6zA\r\na=ice-pwd:1NhU5D47rSz83ANxlY+Tz/XI\r\na=ice-options:trickle\r\na=fingerprint:sha-256 2C:0B:78:49:F5:F0:44:6C:86:DD:27:BC:B6:7D:77:B9:B1:07:F4:2F:37:F5:24:D9:A3:54:7D:B3:A0:3C:57:C0\r\na=setup:active\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n'
但是当库要建立连接时,两个浏览器都返回上述错误。
这是我的代码
//TUTORIAL BASED ON
// https://github.com/feross/simple-peer
var initiator = (location.hash||'') === '#1';
console.log("inititator",location.hash , initiator);
var Peer = require('simple-peer');
params =
initiator: initiator,
trickle: false,
reconnectTimer: 100,
iceTransportPolicy: 'relay',
config:
iceServers: [
urls: "stun:numb.viagenie.ca",
username: "pasaseh@ether123.net",
credential: "12345678"
,
urls: "turn:numb.viagenie.ca",
username: "pasaseh@ether123.net",
credential: "12345678"
]
;
if (typeof window === "undefined")
var wrtc = require('wrtc');
params.wrtc= wrtc;
var p = new Peer(params);
p.on('error', function (err) console.log('error', err) )
p.on('signal', function (data)
console.log('SIGNAL', JSON.stringify(data));
document.querySelector('#outgoing').textContent = JSON.stringify(data)
);
document.querySelector('form').addEventListener('submit', function (ev)
ev.preventDefault();
console.log("am apasat pe button");
p.signal(JSON.parse(document.querySelector('#incoming').value))
);
let index = Math.floor(Math.random()*100);
p.on('connect', function (data)
console.log('CONNECT', data, p);
setInterval(function()
if ((typeof p !== 'undefined')&& ( p !== null))
console.log(p);
p.send('whatever' + index + " ___ " + Math.random())
, 500);
p.on("hello", function(data)
alert(data);
);
p.emit("hello",55);
)
p.on('data', function (data)
console.log('data: ' + data)
);
module.exports = function()
console.log("Hello World Server");
;
webrtc-internals
这个问题有什么解决办法吗?谢谢
【问题讨论】:
【参考方案1】:我建议在列出您的冰服务器时使用仍在工作的冰服务器,如下所示
new SimplePeer(
initiator: false,
tricle: false,
config:
iceServers: [
urls: "turn:numb.viagenie.ca",
credential: "muazkh",
username: "webrtc@live.com",
,
],
,
)
【讨论】:
【参考方案2】:chrome://webrtc-internals
的屏幕截图显示,尽管您使用了 TURN 服务器凭据,但没有收集到中继类型的 icecandidate。详情请见this blog post。
您可以使用this page检查凭据
【讨论】:
菲利普感谢您的信息。我不是 WebRTC 或 Simple Peer 库的专家,但我如何解决源代码中的问题,尤其是因为我提供了 TURN 服务器? TURN 服务器是您必须提供的东西。它们需要花钱才能运行,所以你不能免费获得它们。以上是关于简单对等 WebRTC 错误:Ice 连接失败的主要内容,如果未能解决你的问题,请参考以下文章