I'm trying to follow this example to create DataChannel.
For the signaling I am using websockets, which behave like so:
User A joins
User B joins
User B asks User A to create an offer
-> A opens datachannel before creating an offer
-> A sets his local description
User A sends his offer to User B
-> B answers the offer
-> B sets local and remote description
User B sends A his local description
(User B doesn't directly send message to user A, this is done through websockets with an intermediary server which holds the websocket sessions, skipping that part for sake of simplicity)
The creating offer code for user A is the following:
let pc = new RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.l.google.com:19302",
}
]
});
let dc = pc.createDataChannel("Base");
dc.onbufferedamountlow = function(){
console.error("dataChannel.onbufferedamountlow");
};
dc.onclose = function(){
console.error("dataChannel.onclose");
};
dc.onerror = function(){
console.error("dataChannel.onerror");
};
dc.onmessage = function(){
console.error("dataChannel.onmessage");
};
dc.onopen = function(){
console.error("dataChannel.onopen");
};
let offer = await pc.createOffer();
await pc.setLocalDescription(offer);
let offerString = JSON.stringify(pc.localDescription); //Is sent to B
The complete code for user B is the following:
let pc = new RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.l.google.com:19302",
}
]
});
await pc.setRemoteDescription(new RTCSessionDescription(offer)); //From A
let answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
let answerString = JSON.stringify(pc.localDescription); //Returns to A
So when A receives B's answer, it just:
await pc.setRemoteDescription(new RTCSessionDescription(answer));
Object of A:
Object of B:
What I have also tried without success:
-opening the data channel after both users have established connections, nothing, none of the events get fired, some updates on this:
- after creating the Data Channel on both, I always see readyState as connecting... on both clients...
-opening data channel on B while A is still waiting for B's response back, Data Channel onclose event is fired without even firing the open event...
-using https://test.webrtc.org/ to test, this is the result:
-I've been also looking for other issues or similar to mine, forums, blogs etc... all answers which range from 2 to 5 years old seem way outdated and not working...
-creating the channel on both sides like so:
pc.createDataChannel("Test", {
id: 1,
negotiated: true,
})
Leads to OperationError: Id is in use
on one of the clients. How can I join a channel by Its ID if the DOC states this:
Alternatively (true), they can be negotiated out of-band, where both sides call createDataChannel with an agreed-upon id.
If I must join a channel by an ID but while creating the object I get that Id is in use, wth how do I join it?
If I don't specify an id I get TypeError: id is required when negotiated is true
While the doc says:
ID: Optional - An 16-bit numeric ID for the channel; permitted values are 0-65534. If you don't include this option, the user agent will select an ID for you.
Adding
await pc.addIceCandidate();
orawait pc.addIceCandidate(null);
, after setting both connections Local and Remote description.Started my own TURN server with both TCP and UDP open, same result as with Google's STUN server.
Tried using both STUN and TURN servers in the
iceServer
.Tried both Firefox and Chrome latest versions.
Questions:
1) When must the data channel start opening? After both clients have done negociation and pc.connectionState is stable? Right after creating the connection object? Before or after setting local description ?