Advertisement
silverbucket

WebRTC basic copy&paste 2

Feb 7th, 2015
344
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.     };
  60.  
  61.     channel.onerror = function (err) {
  62.         console.error('** channel.onerror: ', err);
  63.     };
  64.  
  65.     channel.onclose = function (err) {
  66.         console.log('** channel.onclose: ', err);
  67.     };
  68. }
  69.  
  70.  
  71. // ---------------------------------------------------------------------
  72. // Host object creates the offer for a WebRTC session
  73. function Host (id) {
  74.     this.id = id;
  75.     this.peer = new PeerConnection(null, null);
  76.     this.dataChannel = this.peer.createDataChannel(id, {
  77.         reliable: true
  78.     });
  79.     setChannelEvents(this.dataChannel);
  80. }
  81.  
  82. Host.prototype.createOffer = function (cb) {
  83.     var self = this;
  84.  
  85.     this.peer.createOffer(function (offer) {
  86.         console.log('Host.peer.createOffer callback: ', offer);
  87.  
  88.         self.peer.setLocalDescription(offer, function () {
  89.             self.offer = offer;
  90.             cb(offer);
  91.         }, function (err) {
  92.             console.error(err);
  93.         });
  94.     }, function (err) {
  95.         console.error(err);
  96.     }, null);
  97.  
  98. }
  99.  
  100. Host.prototype.setRemoteDescription = function (answer, cb) {
  101.     var self = this;
  102.     console.log('Host.setRemoteDescription: ', answer);
  103.  
  104.     var _sd = new SessionDescription(answer);
  105.  
  106.     this.peer.setRemoteDescription(_sd, cb, function (err) {
  107.         console.error(err);
  108.     });
  109. };
  110.  
  111.  
  112. // ---------------------------------------------------------------------
  113. // Guest object answers the offer from the host
  114. function Guest (id) {
  115.     this.id = id;
  116.     this.peer = new PeerConnection(null, null);
  117.  
  118.     this.dataChannel = this.peer.createDataChannel(id, {
  119.         reliable: true
  120.     });
  121.  
  122.     var self = this;
  123.     this.peer.ondatachannel = function (event) {
  124.         console.log('Guest.peer.datachannel: ', event);
  125.         self.dataChannel = event.channel;
  126.         setChannelEvents(self.dataChannel);
  127.     };
  128. }
  129.  
  130. Guest.prototype.setRemoteDescription = function (offer, cb) {
  131.     var self = this;
  132.     var _desc = new SessionDescription(offer);
  133.     console.log('Guest desc: ', _desc, offer);
  134.    
  135.     this.peer.setRemoteDescription(_desc, function () {
  136.         self.offer = offer;
  137.         cb();
  138.     }, function (err) {
  139.         console.error(err);
  140.     });
  141. };
  142.  
  143. Guest.prototype.createAnswer = function (cb) {
  144.     var self = this;
  145.  
  146.     this.peer.createAnswer(function (answer) {
  147.         console.log('Guest.peer.createAnswer callback: ', answer);
  148.         self.answer = answer;
  149.         self.peer.setLocalDescription(answer, function () {
  150.             cb(answer);
  151.         }, function (err) {
  152.             console.error(err);
  153.         });
  154.     }, function (err) {
  155.         console.error(err);
  156.     });
  157. };
  158.  
  159.  
  160. // ---------------------------------------------------------------------
  161. $(document).ready(function () {
  162.  
  163.     $("#create").click(function () {
  164.         $("#join").attr("disabled", "disabled");
  165.         $("#submitAnswer").removeAttr("disabled");
  166.  
  167.         channelID = $("#channel").val();
  168.         host = new Host(channelID);
  169.         host.createOffer(function (offer) {
  170.             $('#offer').val(JSON.stringify(offer));
  171.         })
  172.         return false;
  173.     });
  174.  
  175.  
  176.     $("#join").click(function () {
  177.         $("#create").attr("disabled", "disabled");
  178.         $("#submitAnswer").attr("disabled", "disabled");
  179.  
  180.         channelID = $("#channel").val();
  181.         offer = $("#offer").val();
  182.         guest = new Guest(channelID);
  183.         guest.setRemoteDescription(JSON.parse(offer), function () {
  184.             console.log('Guest.setRemoteDescription completed');
  185.             guest.createAnswer(function (answer) {
  186.                 $("#answer").val(JSON.stringify(answer));
  187.             });
  188.         });
  189.         return false;
  190.     });
  191.  
  192.     $("#submitAnswer").click(function () {
  193.         var answer = $("#answer").val();
  194.         host.setRemoteDescription(JSON.parse(answer), function () {
  195.             console.log('-- complete');
  196.             host.dataChannel.send('hello world!');
  197.         });
  198.         return false;
  199.     });
  200.  
  201. });
  202.  
  203.         </script>
  204.     </body>
  205. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement