6

I am trying to learn WebRTC . I copied some codes and i get this error:

Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'

Any one can help?

code score: http://www.tutorialspoint.com/webrtc/webrtc_text_demo.htm

GentleMan
  • 77
  • 1
  • 5

2 Answers2

11

Add ondatachannel handling after removing {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();
bronyuk
  • 111
  • 1
  • 3
  • 2
    This should be the answer! Tried this and worked perfectly after trying for hours! Why is this the case? – Kevin Dec 13 '18 at 08:22
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')
  })