samedi 28 février 2015

How do I initiate a WebRTC data channel?


I'm trying to create a WebRTC data channel between clients using web sockets.


I listed some ICE servers



var rtcsettings = {
iceServers: [
{url: "stun:stun.ekiga.net"},
{url: "stun:stun.voipbuster.com"},
{url: "stun:stun.1.google.com:19302"},
]
};


Then there is a connect function that creates a channel and offer and sends it over the web socket.



function(them) {
var pc = new RTCPeerConnection(rtcsettings);
self.channel = pc.createDataChannel("gamelink");
console.log(pc, self.channel);
self.peerconnection = pc;
pc.createOffer(function(offer) {
pc.setLocalDescription(new RTCSessionDescription(offer), function() {
self.socket.send(JSON.stringify({
"to": them,
"uuid": uuid,
"offer": offer,
}));
}, pc.close);
}, pc.close);
}


Then on the other end there is a callback that adds the offer to its connection and sends a response.


It also sets a callback for when a data channel is added, but this never fires. What am I missing?



function (message){
var offer = message.offer;
var pc = new RTCPeerConnection(rtcsettings);
pc.ondatachannel = function(ch) {
self.channel = ch;
console.log(self.channel);
}
console.log(pc);
pc.setRemoteDescription(new RTCSessionDescription(offer), function() {
pc.createAnswer(function(answer) {
pc.setLocalDescription(new RTCSessionDescription(answer), function() {
self.socket.send(JSON.stringify({
"to": message.uuid,
"uuid": uuid,
"answer": answer,
}));
}, pc.close);
}, pc.close);
}, pc.close);
self.peerconnection = pc;
}


Then finally there is another callback on the initiator that adds the response descriptor to its connection.



function(message) {
self.peerconnection.setRemoteDescription(
new RTCSessionDescription(message.answer),
function() { },
self.peerconnection.close);
}


All of the code except the socket stuff and the data channel stuff is taken almost verbatim from the MDN Basic Usage page.


I'm testing with Chrome on localhost, so firewalls should not be a problem.


After the exchange happens, both connections have a local and remote descriptor set. The data channel readyState is connecting. The PeerConnection's iceConnectionState is new and its signalingState is stable.


What's missing?





Aucun commentaire:

Enregistrer un commentaire