javascript - How to start a basic WebRTC data channel? -
how start basic webrtc data channel?
this have far, doesn't seem try , connect. im sure missing basic.
var rtcpeerconnection = window.rtcpeerconnection || window.mozrtcpeerconnection || window.webkitrtcpeerconnection || window.msrtcpeerconnection; var peerconnection = new rtcpeerconnection({ iceservers: [ {url: 'stun:stun1.l.google.com:19302'}, {url: 'stun:stun2.l.google.com:19302'}, {url: 'stun:stun3.l.google.com:19302'}, {url: 'stun:stun4.l.google.com:19302'}, ] }); peerconnection.ondatachannel = function () { console.log('peerconnection.ondatachannel'); }; peerconnection.onicecandidate = function () { console.log('peerconnection.onicecandidate'); }; var datachannel = peerconnection.createdatachannel('mylabel', { }); datachannel.onerror = function (error) { console.log('datachannel.onerror'); }; datachannel.onmessage = function (event) { console.log('datachannel.onmessage'); }; datachannel.onopen = function () { console.log('datachannel.onopen'); datachannel.send('hello world!'); }; datachannel.onclose = function () { console.log('datachannel.onclose'); }; console.log(peerconnection, datachannel);
webrtc assumes have way signal (send offer-string to, , receive answer-string from) whomever wish contact. without server, how that?
to illustrate, here's code (works in firefox , chrome 45):
var config = { iceservers: [{ urls: "stun:stun.l.google.com:19302" }]}; var dc, pc = new rtcpeerconnection(config); pc.ondatachannel = e => { dc = e.channel; dc.onopen = e => (log("chat!"), chat.select()); dc.onmessage = e => log(e.data); } function createoffer() { button.disabled = true; pc.ondatachannel({ channel: pc.createdatachannel("chat") }); pc.createoffer().then(d => pc.setlocaldescription(d)).catch(failed); pc.onicecandidate = e => { if (e.candidate) return; offer.value = pc.localdescription.sdp; offer.select(); answer.placeholder = "paste answer here"; }; }; offer.onkeypress = e => { if (e.keycode != 13 || pc.signalingstate != "stable") return; button.disabled = offer.disabled = true; var obj = { type:"offer", sdp:offer.value }; pc.setremotedescription(new rtcsessiondescription(obj)) .then(() => pc.createanswer()).then(d => pc.setlocaldescription(d)) .catch(failed); pc.onicecandidate = e => { if (e.candidate) return; answer.focus(); answer.value = pc.localdescription.sdp; answer.select(); }; }; answer.onkeypress = e => { if (e.keycode != 13 || pc.signalingstate != "have-local-offer") return; answer.disabled = true; var obj = { type:"answer", sdp:answer.value }; pc.setremotedescription(new rtcsessiondescription(obj)).catch(failed); }; chat.onkeypress = e => { if (e.keycode != 13) return; dc.send(chat.value); log(chat.value); chat.value = ""; }; var log = msg => div.innerhtml += "<p>" + msg + "</p>"; var failed = e => log(e + ", line " + e.linenumber);
<script src="https://rawgit.com/webrtc/adapter/master/adapter.js"></script> <button id="button" onclick="createoffer()">offer:</button> <textarea id="offer" placeholder="paste offer here"></textarea><br> answer: <textarea id="answer"></textarea><br><div id="div"></div> chat: <input id="chat"></input><br>
open page in second tab, , can chat 1 tab other (or different machine around world). stinks must offer there yourself:
- press
offer
button in tab (only) , wait 1-20 seconds till see offer-text, - copy-paste offer-text tab tab b, , hit
enter
- copy-paste answer-text appears tab b tab a, , hit
enter
.
you should able chat between tabs, without server.
as can see, sub-par experience, why need basic websocket server pass offer/answer (as trickle ice candidates if want connecting happen fast) between , b, things started. once have connection, can use data-channels this, little work.
Comments
Post a Comment