6

我正在尝试学习 WebRTC 。我复制了一些代码,我得到了这个错误:

未能在“RTCDataChannel”上执行“发送”:RTCDataChannel.readyState 不是“打开”

任何人都可以帮忙吗?

代码分数:http ://www.tutorialspoint.com/webrtc/webrtc_text_demo.htm

4

2 回答 2

11

ondatachannel删除后添加处理{optional: [{RtpDataChannels: true}]}

  myConnection.onicecandidate = function (event) { 

     if (event.candidate) { 
        send({ 
           type: "candidate", 
           candidate: event.candidate 
        });
     } 
  }; 

  myConnection.ondatachannel = function(event) {
     var receiveChannel = event.channel;
     receiveChannel.onmessage = function(event) {
        console.log("ondatachannel message:", event.data);
     };
  };

  openDataChannel();
于 2016-10-19T07:24:27.827 回答
0

The same error was thrown to me .This is because your peers are not connected and you are sending data.This was solved by this:

peer.on('connect', () => {
    console.log('I am connected now')
    peer.send('sending data blah blah')
  })
于 2020-06-12T06:10:55.633 回答