Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I finally got it to work after sifting through a lot of articles: <a href="http://jsfiddle.net/LcQzV/">http://jsfiddle.net/LcQzV/</a></p> <p>First we create the peer connections:</p> <pre><code>var media = {}; media.fake = media.audio = true; var client = new mozRTCPeerConnection; var server = new mozRTCPeerConnection; </code></pre> <p>When the client connects to the server it must open a data channel:</p> <pre><code>client.onconnection = function () { var channel = client.createDataChannel("chat", {}); channel.onmessage = function (event) { alert("Server: " + event.data); }; channel.onopen = function () { channel.send("Hello Server!"); }; }; </code></pre> <p>When the client creates a data channel the server may respond:</p> <pre><code>server.ondatachannel = function (channel) { channel.onmessage = function (event) { alert("Client: " + event.data); }; channel.onopen = function () { channel.send("Hello Client!"); }; }; </code></pre> <p>We need to add a fake audio stream to the client and the server to establish a connection:</p> <pre><code>navigator.mozGetUserMedia(media, callback, errback); function callback(fakeAudio) { server.addStream(fakeAudio); client.addStream(fakeAudio); client.createOffer(offer); } function errback(error) { alert(error); } </code></pre> <p>The client creates an offer:</p> <pre><code>function offer(description) { client.setLocalDescription(description, function () { server.setRemoteDescription(description, function () { server.createAnswer(answer); }); }); } </code></pre> <p>The server accepts the offer and establishes a connection:</p> <pre><code>function answer(description) { server.setLocalDescription(description, function () { client.setRemoteDescription(description, function () { var port1 = Date.now(); var port2 = port1 + 1; client.connectDataConnection(port1, port2); server.connectDataConnection(port2, port1); }); }); } </code></pre> <p>Phew. That took a while to understand.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload