Javascript 无法通过数据通道获取消息

Javascript 无法通过数据通道获取消息,javascript,webrtc,Javascript,Webrtc,我正在使用WebRTC。我的音频和视频工作正常。我尝试添加一个数据通道 我在ice处理设置之后和创建报价之前使用openDataChannel()函数 // setup ice handling this.myConnection.onicecandidate = (ev) => { if (ev.candidate) { this.send({ type: 'candidate', candidate: ev.can

我正在使用WebRTC。我的音频和视频工作正常。我尝试添加一个数据通道

我在ice处理设置之后和创建报价之前使用openDataChannel()函数

// setup ice handling
this.myConnection.onicecandidate = (ev) => {
    if (ev.candidate) {
        this.send({
            type: 'candidate',
            candidate: ev.candidate
        });
    }
};
this.openDataChannel();
//openDataChannel()

在最新的Chrome和Opera中,数据通道可以正确打开和关闭。但其他用户无法获取消息。没有出现错误

在Firefox中,很奇怪,数据通道正确打开,但7或8秒后,数据通道会自动关闭(音频和视频仍在工作)

这是什么原因造成的?谢谢

下面是
this.dataChannel
this.myConnection


我猜您所犯的错误是在两端创建数据通道,而在另一端通过
ondatachannel
不接受任何一个数据通道,结果是两个悬空的数据通道发送消息,但另一端没有人在听。看看下面的例子,它在chrome和firefox中工作:

“严格使用”;
设pc1=新的RTPeerConnection()
,pc2=新的RTPeerConnection()
,div=document.getElementById('log')
,add=(pc,can)=>can&&pc.addIceCandidate(can).catch(日志)
,log=msg=>div.innerHTML+=“
”+msg ,exchangeSDP=()=>pc1.createOffer()。然后(d=>pc1.setLocalDescription(d)) .then(()=>pc2.setRemoteDescription(pc1.localDescription)) 。然后(()=>pc2.createAnswer())。然后(d=>pc2.setLocalDescription(d)) .then(()=>pc1.setRemoteDescription(pc2.localDescription)) .捕获(日志) ,setChannelEvents=(数据通道,标签)=>{ dataChannel.onerror=log; dataChannel.onclose=()=>log(`datachannelisclosed:${label}`); dataChannel.onmessage=ev=>log(`${label},获取消息:${ev.data}`); dataChannel.onopen=()=>log(`datachannelisopened:${label}`); setTimeout(()=>dataChannel.send('Hello from'+label'),3000); } pc1.onicecandidate=e=>add(pc2,e.candidate); pc2.onicecandidate=e=>add(pc1,e.candidate); pc1.oniceconnectionstatechange=e=>log(pc1.iceConnectionState); setChannelEvents(pc1.createDataChannel('label',{reliable:true}),'Peer1'); pc2.ondatachannel=evt=>setChannelEvents(evt.channel,'Peer2'); exchangeSDP()


您是否在创建优惠之前创建了数据频道?您好@mido For caller,是的,我在创建优惠之前创建了数据频道。对于被叫方,我在收到报价后创建了datachannel。我的控制台可以显示“数据通道已打开”,但无法收到消息谢谢,@mido,我想你的猜测可能是正确的。但是在代码中,唯一能确保一个用户与另一个正确用户连接的是使用
this.myConnection.createDataChannel
时。我错过什么了吗?(我还在我的问题中添加了
this.myConnection
的屏幕截图,这与控制台中显示的代码中的
pc1
pc2
相同。)嗯,我有没有办法知道有人是否与此用户连接,以及谁与此用户在数据频道中连接?哇,这是我的错误,我看不清楚,
ondatachannel
不应总是
null
!也许这就是我的代码运行不好的原因。我将设法找出错误所在。谢谢
openDataChannel() {
    let dataChannelOptions = {
        reliable: true
    };
    this.dataChannel = this.myConnection.createDataChannel('myLabel', dataChannelOptions);

    this.dataChannel.onerror = (err) => {
        console.log(err);
    };

    this.dataChannel.onmessage = (ev) => {
        console.log('Got message ', ev.data);
    };

    this.dataChannel.onopen = () => {
        console.log('Data Channel is opened.');
        console.log('this.dataChannel', this.dataChannel);   // Screenshot below
        console.log('this.myConnection', this.myConnection); // Screenshot below
        this.dataChannel.send('Hello!');
    };

    this.dataChannel.onclose = () => {
        console.log('Data Channel is closed.');
    };
}