i'm trying connect 2 peers webrtc , datachannel without camera , microphone.
try { socket = new websocket("ws://localhost:1337/"); var servers = {iceservers:[{url:"stun:stun.l.google.com:19302"}]}; peerconn = new webkitrtcpeerconnection(servers, {optional:[{rtpdatachannels: true}]}); channel = peerconn.createdatachannel("abcd1234", {reliable: false}); peerconn.onicecandidate = function(evt) { if(evt.candidate) { socket.send(json.stringify({"candidate": evt.candidate})); } }; channel.onopen = function () { console.log("channel open"); channel.send('first text message on rtp data ports'); }; channel.onmessage = function (event) { console.log('received message:', event.data); }; peerconn.createoffer(function(desc) { peerconn.setlocaldescription(desc); socket.send(json.stringify({"sdp": desc})); }); socket.onmessage = function(evt) { var signal = json.parse(evt.data); if(signal.sdp) { peerconn.setremotedescription(new rtcsessiondescription(signal.sdp)); alert("desc"); } else { peerconn.addicecandidate(new rtcicecandidate(signal.candidate)); alert("ice"); } } } catch(e) { console.log(e.message); }
in chrome errors out with:
uncaught error: invalidstateerror: dom exception 11
open 2 tabs; click "create offer" button 1st tab; , watch console logs:
<script> // webkitrtcpeerconnection && rtcdatachannel specific code goes here var iceservers = { iceservers: [{ url: 'stun:stun.l.google.com:19302' }] }; var optionalrtpdatachannels = { optional: [{ rtpdatachannels: true }] }; var mediaconstraints = { optional: [], mandatory: { offertoreceiveaudio: false, // hmm!! offertoreceivevideo: false // hmm!! } }; var offerer, answerer, answererdatachannel, offererdatachannel; function createoffer() { offerer = new webkitrtcpeerconnection(iceservers, optionalrtpdatachannels); offererdatachannel = offerer.createdatachannel('rtcdatachannel', { reliable: false }); setchannelevents(offererdatachannel, 'offerer'); offerer.onicecandidate = function (event) { if (!event.candidate) returnsdp(); }; offerer.ongatheringchange = function (event) { if (event.currenttarget && event.currenttarget.icegatheringstate === 'complete') returnsdp(); }; function returnsdp() { socket.send({ sender: 'offerer', sdp: offerer.localdescription }); } offerer.createoffer(function (sessiondescription) { offerer.setlocaldescription(sessiondescription); }, null, mediaconstraints); } function createanswer(offersdp) { answerer = new webkitrtcpeerconnection(iceservers, optionalrtpdatachannels); answererdatachannel = answerer.createdatachannel('rtcdatachannel', { reliable: false }); setchannelevents(answererdatachannel, 'answerer'); answerer.onicecandidate = function (event) { if (!event.candidate) returnsdp(); }; answerer.ongatheringchange = function (event) { if (event.currenttarget && event.currenttarget.icegatheringstate === 'complete') returnsdp(); }; function returnsdp() { socket.send({ sender: 'answerer', sdp: answerer.localdescription }); } answerer.setremotedescription(new rtcsessiondescription(offersdp)); answerer.createanswer(function (sessiondescription) { answerer.setlocaldescription(sessiondescription); }, null, mediaconstraints); } function setchannelevents(channel, channelnameforconsoleoutput) { channel.onmessage = function (event) { console.debug(channelnameforconsoleoutput, 'received message:', event.data); }; channel.onopen = function () { channel.send('first text message on rtp data ports'); }; } // websocket specific code goes here var socket = new websocket('ws://localhost:1337'); socket.onmessage = function (e) { var data = json.parse(e.data); console.log(data); if (data.sdp) { if (data.sender == 'offerer') createanswer(data.sdp); else offerer.setremotedescription(new rtcsessiondescription(data.sdp)); } }; socket.push = socket.send; socket.send = function (data) { socket.push(json.stringify(data)); }; </script> <button id="create-offer">create offer</button> <script> document.getelementbyid('create-offer').onclick = function () { this.disabled = true; createoffer(); }; </script>
Comments
Post a Comment