我试图使用简单的对等库来建立浏览器到浏览器的WebRTC连接(数据通道)我的理解是(也许我有一些误解)为了让两个浏览器通过WebRTC连接,它们必须交换SDP数据并执行NAT遍历为此,可以实现STUN服务器。
在simple peer库中,它们声明simple peer不实现信令协议,但它确实提供了一种提供STUN/ICE服务器的方法从网格示例中考虑以下三个HTML文件:
peer1.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Peer1</title>
</head>
<body>
<script src="../../js/simplepeer.min.js"></script>
<script>
// These are peer1's connections to peer2 and peer3
var peer2 = new SimplePeer({ initiator: true, config: {
iceServers: [
{urls: 'stun:stun.a-mm.tv:3478'}
]
} })
var peer3 = new SimplePeer({ initiator: true, config: {
iceServers: [
{urls: 'stun:stun.a-mm.tv:3478'}
]
} })
peer2.on('signal', data => {
console.log(data)
})
peer2.on('connect', () => {
peer2.send('hi peer2, this is peer1')
})
peer2.on('data', data => {
console.log('got a message from peer2: ' + data)
})
peer3.on('signal', data => {
console.log(data)
})
peer3.on('connect', () => {
peer3.send('hi peer3, this is peer1')
})
peer3.on('data', data => {
console.log('got a message from peer3: ' + data)
})
</script>
</body>
</html>```
对等2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Peer2</title>
</head>
<body>
<script src="../../js/simplepeer.min.js"></script>
<script>
// These are peer2's connections to peer1 and peer3
var peer1 = new SimplePeer({
config: {
iceServers: [
{urls: 'stun:stun.a-mm.tv:3478'}
]
}
})
var peer3 = new SimplePeer({ initiator: true, config: {
iceServers: [
{urls: 'stun:stun.a-mm.tv:3478'}
]
} })
peer1.on('signal', data => {
console.log(data)
})
peer1.on('connect', () => {
peer1.send('hi peer1, this is peer2')
})
peer1.on('data', data => {
console.log('got a message from peer1: ' + data)
})
peer3.on('signal', data => {
console.log(data)
})
peer3.on('connect', () => {
peer3.send('hi peer3, this is peer2')
})
peer3.on('data', data => {
console.log('got a message from peer3: ' + data)
})
</script>
</body>
</html>
对等3.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Peer3</title>
</head>
<body>
<script src="../../js/simplepeer.min.js"></script>
<script>
// These are peer3's connections to peer1 and peer2
var peer1 = new SimplePeer({
config: {
iceServers: [
{urls: 'stun:stun.a-mm.tv:3478'}
]
}
}
)
var peer2 = new SimplePeer({
config: {
iceServers: [
{urls: 'stun:stun.a-mm.tv:3478'}
]
}
})
peer1.on('signal', data => {
console.log(data)
})
peer1.on('connect', () => {
peer1.send('hi peer1, this is peer3')
})
peer1.on('data', data => {
console.log('got a message from peer1: ' + data)
})
peer2.on('signal', data => {
console.log(data)
})
peer2.on('connect', () => {
peer2.send('hi peer2, this is peer3')
})
peer2.on('data', data => {
console.log('got a message from peer2: ' + data)
})
</script>
</body>
</html>
你可以看到我在那里添加了两个眩晕服务器STUN不交换必要的信令数据吗然而,在研究他们的问题时,他们建议使用websockets来交换这些数据那么,我是否可以假设STUN只是为了让每个对等方都可以首先收集SDP信息,然后必须使用websockets来交换它然后,在那之后,浏览器可以互相建立数据通道?
为什么我认为史登处理了这件事当STUN已经能够向客户端提供数据时,引入websockets似乎有点多余当然,我们的目标是尽快停止所有的中央服务器通信,转而使用浏览器到浏览器的数据通道。
一个好的答案可以回答这些问题并修改示例。