如何解决stun服务无法打洞建立p2p连接的问题
大家好,今天给大家介绍一点网络通信相关的内容,主要涉及到网络路由相关知识,希望对您有所帮助!
问题描述:stun服务无法在对称型nat间实现点对点跨网络通信。
解决办法:自行搭建tun服务,退而求其次,实现流的转发,大体如下:
服务搭建:
部分代码:
const c = e.candidate;let type = 'unknown';if (c.candidate.includes('typ relay')) type = 'relay';else if (c.candidate.includes('typ srflx')) type = 'srflx';else if (c.candidate.includes('typ host')) type = 'host';const parts = c.candidate.split(' ');const protocol = parts[2].toLowerCase();const ip = parts[4];const port = parts[5];iceLog(`${type.padEnd(7)} ${ip}:${port} ${protocol}`, type);// 缓存所有ICE候选iceCandidateQueue.push(c);debugLog(`ICE候选已缓存 (总数: ${iceCandidateQueue.length})`);
............socket.onmessage = async (ev) => {try {const data = JSON.parse(ev.data);debugLog(`收到消息: ${data.type} 来自: ${data.senderId || '未知'}`);switch (data.type) {case 'request-connection':if (myRole === 'controlled' && pendingOffer) {if (data.senderId) {debugLog('控制端请求连接,发送 offer 到: ' + data.senderId);lastSenderId = data.senderId;// 发送offersocket.send(JSON.stringify({type: 'offer', offer: pendingOffer, targetId: data.senderId, senderId: myId}));
更多内容,您可以去 B站大衣哥编程 进一步查看,如果您对编程感兴趣,特别是C/C++、C#、Java、Go、分布式、流媒体、计算机网络、协议分析、数据库等,可戳此连接