无法在 node.js 中建立对等 webrtc 连接

Posted

技术标签:

【中文标题】无法在 node.js 中建立对等 webrtc 连接【英文标题】:Cannot establish peer to peer webrtc connection in node.js 【发布时间】:2022-01-18 21:04:01 【问题描述】:

我正在使用一个名为 wrtc 的 WebRTC npm 包和 node.js 的内置网络库。由于某种原因,我无法建立连接。我得到了错误:

C:\dev\WebRTCchat\node_modules\wrtc\lib\index.js:49
  this._send(data);
       ^
DOMException [InvalidStateError]: RTCDataChannel.readyState is not 'open
'
    at RTCDataChannel.send (C:\dev\WebRTCchat\node_modules\wrtc\lib\inde
x.js:49:8)
    at C:\dev\WebRTCchat\clientA.js:35:35

有什么想法吗?所以这里是客户 A 代码(谁创建了报价)

clientA.js

const net = require('net');
const  RTCPeerConnection  = require("wrtc");

const connA = new RTCPeerConnection();
connA.onicecandidate = () => console.log("Ice candidate added. ");// + JSON.stringify(connA.localDescription));
const dc = connA.createDataChannel("channel");
dc.onmessage = e => console.log("Message -> " + e.data);
dc.onopen = () => console.log("Client connected!");

function delay(ms) 
    return new Promise(res => setTimeout(res, ms));


function createSDPOffer() 
    return new Promise(resolve => connA.createOffer()
        .then(o => 
            connA.setLocalDescription(o);
            console.log("Local Description set!");
            resolve(o);
        ));


function getSDPAnswer(answer) 
    connA.setRemoteDescription(answer).then(a => console.log("Remote description set!"));


const server = net.createServer();
server.on('connection', socket => 
    createSDPOffer().then(offer => 
        socket.write(JSON.stringify(offer));
    )

    socket.on('data', data => 
        getSDPAnswer(JSON.parse(data));
        delay(1000).then(() => dc.send("Hello I am client A"));
    )
)

server.listen(1337, '127.0.0.1');

然后是客户 B(收到报价并创建答案的人)

clientB.js

const net = require('net');
const  RTCPeerConnection  = require("wrtc");

const connB = new RTCPeerConnection();
const socket = new net.Socket();

function delay(ms) 
    return new Promise(res => setTimeout(res, ms));


socket.connect(1337, "127.0.0.1");

socket.on('data', (data) => 
    getSDPOffer(JSON.parse(data));

    sendSDPAnswer().then(a => socket.write(JSON.stringify(a)));

    delay(2000).then(() => 
        connB.dc.send("Hello I am B")
    );

);

socket.on('close', () => 

)


function getSDPOffer(offer) 
    connB.onicecandidate = () => console.log("Ice candidate added. "); //+ JSON.stringify(connB.localDescription));

    connB.ondatachannel = e => 
        connB.dc = e.channel;
        connB.dc.onopen = () => console.log("Client connected!");
        connB.dc.onmessage = e => console.log("Message -> " + e.data);
    

    connB.setRemoteDescription(offer)
        .then(() => console.log("Remote Description Offer set!"));


function sendSDPAnswer() 
    return new Promise(resolve => 
        connB.createAnswer()
            .then(a => 
                connB.setLocalDescription(a);
                console.log("Local Description set!");
                resolve(a);
            );
    )

【问题讨论】:

ice候补需要在远端交换和添加,而不是登录。 您能否提供更多关于如何做到这一点的信息? 【参考方案1】:

我发现使用 connA.localDescription 而不是 createOffer 创建的 offer 对象,一切正常。不过不知道为什么..

function createSDPOffer() 
    return connA.createOffer()
        .then(o => 
            connA.setLocalDescription(o);
            console.log("Local Description set!");
        );


...

const server = net.createServer();
server.on('connection', socket => 
    createSDPOffer().then(() => 
        socket.write(JSON.stringify(connA.localDescription));
    )

【讨论】:

以上是关于无法在 node.js 中建立对等 webrtc 连接的主要内容,如果未能解决你的问题,请参考以下文章

WebRTC 对等服务器连接

PHP棘轮和WebRTC

如何在对等js中建立对等点之间的连接

即使在使用 webRTC 建立对等连接后停止流后,浏览器仍继续使用红点访问相机

检测 WebRTC 连接中的离线对等点

Android WebView 无法在 WebRTC 对等连接上显示两个视频标签