WebRTC在本地工作,但不能通过Internet工作

WebRTC在本地工作,但不能通过Internet工作,webrtc,Webrtc,我正在尝试一个简单的概念验证WebRTC应用程序。我的代码可以在本地网络上运行,但不能在互联网上运行 我相信我有一个错误,但我想不出来 server.js(expressjs) require('dotenv').config(); const express=require('express'); 常量app=express(); const http=require('http')。createServer(应用程序); const io=require('socket.io')(http)

我正在尝试一个简单的概念验证WebRTC应用程序。我的代码可以在本地网络上运行,但不能在互联网上运行

我相信我有一个错误,但我想不出来

server.js(expressjs)

require('dotenv').config();
const express=require('express');
常量app=express();
const http=require('http')。createServer(应用程序);
const io=require('socket.io')(http);
应用程序使用(express.static('public'));
io.on('连接',(套接字)=>{
on('join',()=>socket.broadcast.emit('join',socket.id));
socket.on('offer',(id,sdp)=>socket.to(id).emit('offer',socket.id,sdp));
socket.on('answer',(id,sdp)=>socket.to(id).emit('answer',socket.id,sdp));
socket.on('candidate',(id,candidate)=>socket.to(id).emit('candidate',socket.id,candidate));
});
const port=process.env.port | | 443;
http.listen(端口,()=>console.log(“侦听:+port”);
client.js(内置网页包)

从“socket.io客户端”导入io;
log(“获取视频流”);
navigator.mediaDevices.getUserMedia({video:true})。然后(myStream=>{
const socket=io.connect();
console.log(“连接”);
emit('join');
让连接={};
socket.on('join',异步对等\u id=>{
const connection=newConnection(对等方id);
console.log(“创建报价”);
const sdp=wait connection.createOffer();
console.log('settinglocaldesc');
等待连接。setLocalDescription(sdp);
console.log(“发送报价”);
emit('offer',peer\u id,connection.localDescription);
});
socket.on('offer',异步(对等id,remoteDescription)=>{
const connection=newConnection(对等方id);
console.log('settingremotedesc');
等待连接。setRemoteDescription(remoteDescription);
log(“创建应答”);
const sdp=wait connection.createAnswer();
console.log('settinglocaldesc');
等待连接。setLocalDescription(sdp);
console.log(“发送应答”);
emit('answer',peer_id,connection.localDescription);
});
socket.on('answer',(对等id,remoteDescription)=>{
console.log('settingremotedesc');
连接[peer_id].setRemoteDescription(remoteDescription);
});
socket.on('candidate',(peer_id,candidate)=>{
console.log('addingcandidate');
连接[peer_id]。addIceCandidate(候选);
});
函数newConnection(对等\u id){
log(“创建连接”);
const connection=新的RTPeerConnection(
{ICEServer:[{URL:['stun:stun.l.google.com:19302']}]
);
连接[peer_id]=连接;
connection.onconnectionstatechange=()=>console.log('connection state',connection.connectionState);
console.log(“搜索候选人”);
connection.onicecandidate=事件=>{
if(事件候选){
console.log('sending candidate');
socket.emit('candidate',peer_id,event.candidate);
}否则{
console.log(“找到的所有候选对象”);
}
};
log(“监听音轨”);
connection.ontrack=事件=>{
console.log('track received');
const stream=新媒体流();
stream.addTrack(event.track);
document.getElementById('video').srcObject=stream;
};
for(myStream.getTracks()的常量跟踪){
console.log('sending track');
连接。添加跟踪(跟踪);
}
回路连接;
}
});
来自本地网络对等方(已连接)的日志 第一个同伴

getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
creating offer
setting local desc
sending offer
(2) sending candidate
setting remote desc
adding candidate
track received
connection state connecting
all candidates found
connection state connected
getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
creating offer
setting local desc
sending offer
(3) sending candidate
all candidates found
setting remote desc
track received
connection state connecting
(5) adding candidate
connection state failed
第二同伴

getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
setting remote desc
track received
creating answer
setting local desc
sending answer
sending candidate
connection state connecting
all candidates found
connection state connected
(2) adding candidate
getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
setting remote desc
track received
creating answer
setting local desc
sending answer
(5) sending candidate
(3) adding candidate
connection state connecting
connection state failed
all candidates found
来自Internet上对等方的日志(失败) 第一个同伴

getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
creating offer
setting local desc
sending offer
(2) sending candidate
setting remote desc
adding candidate
track received
connection state connecting
all candidates found
connection state connected
getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
creating offer
setting local desc
sending offer
(3) sending candidate
all candidates found
setting remote desc
track received
connection state connecting
(5) adding candidate
connection state failed
第二同伴

getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
setting remote desc
track received
creating answer
setting local desc
sending answer
sending candidate
connection state connecting
all candidates found
connection state connected
(2) adding candidate
getting video stream
joining
creating connection
searching for candidates
listening for tracks
sending track
setting remote desc
track received
creating answer
setting local desc
sending answer
(5) sending candidate
(3) adding candidate
connection state connecting
connection state failed
all candidates found

除非使用TURN,否则WebRTC不会处理所有连接对


您将看到一些同行能够连接,但您可以在我的回答中了解到所有重要的案例

难道我不能至少连接一个客户端组合吗?我已经尝试过我的家庭互联网、我的手机5g和一个Verizon热点的排列。有什么特别的东西我应该在里面寻找吗chrome://webrtc-internals/? 我在每个客户机上看到两个udp和一个tcp(ipv4)候选。他们被适当地添加到他们的同龄人中。如果有什么方法可以让我看到这些候选人中的任何一个的状态呢?我试了一些Twilio turn服务器,代码运行正常。所以,我想我可以放弃这里有虫子的希望了。谢谢你的帮助。很乐意帮忙!很难追踪,因为它取决于每个连接(它们之间的交互可能不同)。如果您有两个使用同一ISP的对等方,它们可能具有不同的NAT类型等。。。是一个很酷的工具,可能会有帮助!