Advertisement
Guest User

Untitled

a guest
Feb 8th, 2015
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <title>WebRTC Basic</title>
  4.     </head>
  5.     <body>
  6.         <header><h1>WebRTC Basic</h1></header>
  7.  
  8.         <section>
  9.  
  10.             <div id="info" class="disconnected">
  11.                 you must first create or join a channel
  12.             </div>
  13.  
  14.             <form id="signal" action="">
  15.                 <p>
  16.                     <label for="channel">channel: </label>
  17.                     <input id="channel" type="text" value="test" />
  18.                     <button id="create">create</button>
  19.                 </p>
  20.  
  21.                 <p>
  22.                     <label for="offer">offer: </label>
  23.                     <textarea id="offer" rows="8" cols="60"></textarea>
  24.                     <button id="join">join</button>
  25.                 </p>
  26.  
  27.                 <p>
  28.                     <label for="answer">answer: </label>
  29.                     <textarea id="answer" rows="8" cols="60"></textarea>
  30.                     <button id="submitAnswer" disabled>submit answer</button>
  31.                 </p>
  32.             </form>
  33.  
  34.             <form id="chat" action="">
  35.                 <label for="message"> message: </label>
  36.                 <input id="message" autocomplete="off" disabled />
  37.                 <button id="send" disabled>send</button>
  38.             </form>
  39.  
  40.             <ul id="messages"></ul>
  41.  
  42.         </section>
  43.         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  44.         <script type="text/javascript">
  45.  
  46.             var host, guest;
  47.  
  48.             var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
  49.             var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
  50.             var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
  51.  
  52.             function setChannelEvents(channel) {
  53.                 channel.onmessage = function (event) {
  54.                     console.log('** channel.onmessage: ', event);
  55.                 };
  56.  
  57.                 channel.onopen = function () {
  58.                     console.log('** channel.onopen');
  59.                     // and finally we can do this
  60.                     channel.send('hello world!');
  61.  
  62.                 };
  63.  
  64.                 channel.onerror = function (err) {
  65.                     console.error('** channel.onerror: ', err);
  66.                 };
  67.  
  68.                 channel.onclose = function (err) {
  69.                     console.log('** channel.onclose: ', err);
  70.                 };
  71.             }
  72.  
  73.  
  74. // ---------------------------------------------------------------------
  75. // Host object creates the offer for a WebRTC session
  76.             function Host(id) {
  77.                 var self = this;
  78.                 this.id = id;
  79.                 this.peer = new PeerConnection(null, null);
  80.                 this.dataChannel = this.peer.createDataChannel(id, {
  81.                     reliable: true
  82.                 });
  83.  
  84.                 this.peer.onicecandidate = function (event) {
  85.                     if (!event.candidate) { // when ice is complete, there is an event without a candidate (on Chrome anyhow)
  86.                         console.log(self.peer);
  87.                         self.offer = self.peer.localDescription;
  88.                         self.cb(self.offer);
  89.                     }
  90.                 }
  91.  
  92.                 setChannelEvents(this.dataChannel);
  93.             }
  94.  
  95.             Host.prototype.createOffer = function (cb) {
  96.                 var self = this;
  97.  
  98.                 this.peer.createOffer(function (offer) {
  99.                     console.log('Host.peer.createOffer callback: ', offer);
  100.                     self.cb = cb;
  101.                     self.peer.setLocalDescription(offer, function () {
  102.                         // We wait for ICE to complete before continuing
  103.                         //self.offer = offer;
  104.                         //cb(offer);
  105.                     }, function (err) {
  106.                         console.error(err);
  107.                     });
  108.                 }, function (err) {
  109.                     console.error(err);
  110.                 }, null);
  111.  
  112.             }
  113.  
  114.             Host.prototype.setRemoteDescription = function (answer, cb) {
  115.                 var self = this;
  116.                 console.log('Host.setRemoteDescription: ', answer);
  117.  
  118.                 var _sd = new SessionDescription(answer);
  119.  
  120.                 this.peer.setRemoteDescription(_sd, cb, function (err) {
  121.                     console.error(err);
  122.                 });
  123.             };
  124.  
  125.  
  126. // ---------------------------------------------------------------------
  127. // Guest object answers the offer from the host
  128.             function Guest(id) {
  129.                 this.id = id;
  130.                 this.peer = new PeerConnection(null, null);
  131.  
  132.                 this.dataChannel = this.peer.createDataChannel(id, {
  133.                     reliable: true
  134.                 });
  135.  
  136.                 var self = this;
  137.                 this.peer.ondatachannel = function (event) {
  138.                     console.log('Guest.peer.datachannel: ', event);
  139.                     self.dataChannel = event.channel;
  140.                     setChannelEvents(self.dataChannel);
  141.                 };
  142.                 this.peer.onicecandidate = function (event) {
  143.                     if (!event.candidate) { // when ice is complete, there is an event without a candidate (on Chrome anyhow)
  144.                         self.answer = self.peer.localDescription;
  145.                         self.cb(self.answer);
  146.                     }
  147.                 }
  148.             }
  149.  
  150.             Guest.prototype.setRemoteDescription = function (offer, cb) {
  151.                 var self = this;
  152.                 var _desc = new SessionDescription(offer);
  153.                 console.log('Guest desc: ', _desc, offer);
  154.  
  155.                 this.peer.setRemoteDescription(_desc, function () {
  156.                     self.offer = offer;
  157.                     cb();
  158.                 }, function (err) {
  159.                     console.error(err);
  160.                 });
  161.             };
  162.  
  163.             Guest.prototype.createAnswer = function (cb) {
  164.                 var self = this;
  165.  
  166.                 this.peer.createAnswer(function (answer) {
  167.                     console.log('Guest.peer.createAnswer callback: ', answer);
  168.                     self.answer = answer;
  169.                     self.cb = cb;
  170.                     self.peer.setLocalDescription(answer, function () {
  171.                         // Again, waiting for ICE
  172.                         //cb(answer);
  173.                     }, function (err) {
  174.                         console.error(err);
  175.                     });
  176.                 }, function (err) {
  177.                     console.error(err);
  178.                 });
  179.             };
  180.  
  181.  
  182. // ---------------------------------------------------------------------
  183.             $(document).ready(function () {
  184.  
  185.                 $("#create").click(function () {
  186.                     $("#join").attr("disabled", "disabled");
  187.                     $("#submitAnswer").removeAttr("disabled");
  188.  
  189.                     channelID = $("#channel").val();
  190.                     host = new Host(channelID);
  191.                     host.createOffer(function (offer) {
  192.                         $('#offer').val(JSON.stringify(offer));
  193.                     })
  194.                     return false;
  195.                 });
  196.  
  197.  
  198.                 $("#join").click(function () {
  199.                     $("#create").attr("disabled", "disabled");
  200.                     $("#submitAnswer").attr("disabled", "disabled");
  201.  
  202.                     channelID = $("#channel").val();
  203.                     offer = $("#offer").val();
  204.                     guest = new Guest(channelID);
  205.                     guest.setRemoteDescription(JSON.parse(offer), function () {
  206.                         console.log('Guest.setRemoteDescription completed');
  207.                         guest.createAnswer(function (answer) {
  208.                             $("#answer").val(JSON.stringify(answer));
  209.                         });
  210.                     });
  211.                     return false;
  212.                 });
  213.  
  214.                 $("#submitAnswer").click(function () {
  215.                     var answer = $("#answer").val();
  216.                     host.setRemoteDescription(JSON.parse(answer), function () {
  217.                         console.log('-- complete');
  218.                         // We wait before the data channel is set up, before we transmit anything (peer.onremotedatachannel)
  219.                         //host.dataChannel.send('hello world!');
  220.                     });
  221.                     return false;
  222.                 });
  223.  
  224.             });
  225.  
  226.         </script>
  227.     </body>
  228. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement