Reputation: 515
Is there a way to create data channel when peer connection already established?
Here is what I'm doing:
peerConnection.onstatechange = function(event){
var state = peerConnection.readyState;
console.log('state changed to : '+state);
if(state==='stable'){
console.log('connection is stable');
var dataChannel = peerConnection.createDataChannel('test',{reliable: false});
dataChannel.onopen = function(){
console.log('data channel opened');
dataChannel.send('hello data channel');
};
peerConnection.ondatachannel = function(event){
console.log('ondatachannel event fire ',event);
};
}
};
And it gives me the following output:
state changed to : have-local-offer
state changed to : stable
connection is stable
How to make sure that connection is really established? I found here that stable
state is equal to active
sate.
onopen
event does not fire for some reason, so I'm still not sure that connection is established.
If you need some more code, tell me.
EDIT:
I added onnegotiationneeded
event handler and now peerConnection.ondatachannel
is firing, but the channel is in connecting
state
Upvotes: 3
Views: 3911
Reputation: 385
You might have run into the same problem as I have. Apparently you must add a datachannel before you create an offer, and if you want to add a stream you will need to create a new offer/answer and renegotiate. According to the W3C specs:
In particular, if a RTCPeerConnection object is consuming a MediaStream and a track is added to one of the stream's MediaStreamTrackList objects, by, e.g., the add() method being invoked, the RTCPeerConnection object must fire the "negotiationneeded" event. Removal of media components must also trigger "negotiationneeded".
However, the negotiationneeded event is not yet built in FF (and I think in Chrome to) so that's why things might behave a bit erratically.
Upvotes: 11