7

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
  • 87
  • 1
  • 5
  • How are you running your application??? – Umair Shah Aug 17 '16 at 00:18
  • Have you installed all required libraries for your app through `npm install` ?? – Umair Shah Aug 17 '16 at 00:19
  • I believe this should help : http://stackoverflow.com/questions/22470291/rtcdatachannels-readystate-is-not-open – Umair Shah Aug 17 '16 at 00:22
  • Also take a look over the MDN too : https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState – Umair Shah Aug 17 '16 at 00:22
  • Share your full code...That's the Tutorial link..! – Umair Shah Aug 17 '16 at 00:24
  • I run the server with node (cmd) and the html file in the browers. I did install via npm. I looked at these links and i cound find any help there. – GentleMan Aug 17 '16 at 00:29
  • my code is exectly the code from the totorial. i just copied it – GentleMan Aug 17 '16 at 00:31
  • i have tried anothr totorial at : http://www.tutorialspoint.com/webrtc/webrtc_sending_messages.htm i have the same problem. what am i missing ? – GentleMan Aug 17 '16 at 01:42
  • 1
    Those tutorials are old! Try removing `{optional: [{RtpDataChannels: true}]}`. That old Chrome-only stuff no longer works. – jib Aug 17 '16 at 02:49
  • Well I cant see the original error any more but the program still wont function correctly. The message was not transferred . What is wrong ? Maybe its connected to another error i get " Uncaught SyntaxError: Unexpected token H in JSON at position 0 " ? – GentleMan Aug 17 '16 at 09:33
  • after removing this line it seems to send the data but on the other side of the datachanle the onmmesege even wont triger – GentleMan Aug 17 '16 at 11:57

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')
  })
Sachin Negi
  • 51
  • 1
  • 4