chat.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>PeerJS chat demo</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="Content-Language" content="en-us">
  7. <link href="fancy.css" rel="stylesheet" type="text/css">
  8. <script>
  9. // Just for demo.
  10. console._log = console.log;
  11. console.log = function() {
  12. var copy = Array.prototype.slice.call(arguments).join(' ');
  13. $('.log').append(copy + '<br>');
  14. console._log(copy);
  15. };
  16. </script>
  17. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  18. <script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script>
  19. <script>
  20. // Connect to PeerJS, have server assign an ID instead of providing one
  21. var peer = new Peer({key: 'lwjd5qra8257b9', debug: true});
  22. // Show this peer's ID.
  23. peer.on('open', function(id){
  24. $('#pid').text(id);
  25. });
  26. // Await connections from others
  27. peer.on('connection', connect);
  28. // Handle a connection object.
  29. function connect(c) {
  30. // Handle a chat connection.
  31. if (c.label === 'chat') {
  32. var chatbox = $('<div></div>').addClass('connection').addClass('active').attr('id', c.peer);
  33. var header = $('<h1></h1>').html('Chat with <strong>' + c.peer + '</strong>');
  34. var messages = $('<div><em>Peer connected.</em></div>').addClass('messages');
  35. chatbox.append(header);
  36. chatbox.append(messages);
  37. // Select connection handler.
  38. chatbox.on('click', function() {
  39. if ($(this).attr('class').indexOf('active') === -1) {
  40. $(this).addClass('active');
  41. } else {
  42. $(this).removeClass('active');
  43. }
  44. });
  45. $('.filler').hide();
  46. $('#connections').append(chatbox);
  47. c.on('data', function(data) {
  48. messages.append('<div><span class="peer">' + c.peer + '</span>: ' + data +
  49. '</div>');
  50. });
  51. c.on('close', function() {
  52. alert(c.peer + ' has left the chat.');
  53. chatbox.remove();
  54. if ($('.connection').length === 0) {
  55. $('.filler').show();
  56. }
  57. });
  58. } else if (c.label === 'file') {
  59. c.on('data', function(data) {
  60. // If we're getting a file, create a URL for it.
  61. if (data.constructor === ArrayBuffer) {
  62. var dataView = new Uint8Array(data);
  63. var dataBlob = new Blob([dataView]);
  64. var url = window.URL.createObjectURL(dataBlob);
  65. $('#' + c.peer).find('.messages').append('<div><span class="file">' +
  66. c.peer + ' has sent you a <a target="_blank" href="' + url + '">file</a>.</span></div>');
  67. }
  68. });
  69. }
  70. }
  71. $(document).ready(function() {
  72. // Prepare file drop box.
  73. var box = $('#box');
  74. box.on('dragenter', doNothing);
  75. box.on('dragover', doNothing);
  76. box.on('drop', function(e){
  77. e.originalEvent.preventDefault();
  78. var file = e.originalEvent.dataTransfer.files[0];
  79. eachActiveConnection(function(c, $c) {
  80. if (c.label === 'file') {
  81. c.send(file);
  82. $c.find('.messages').append('<div><span class="file">You sent a file.</span></div>');
  83. }
  84. });
  85. });
  86. function doNothing(e){
  87. e.preventDefault();
  88. e.stopPropagation();
  89. }
  90. // Connect to a peer
  91. $('#connect').click(function() {
  92. // Create 2 connections, one labelled chat and another labelled file.
  93. var c = peer.connect($('#rid').val(), { label: 'chat' });
  94. c.on('open', function() {
  95. connect(c);
  96. });
  97. c.on('error', function(err) { alert(err); });
  98. var f = peer.connect($('#rid').val(), { reliable: true, label: 'file' });
  99. f.on('open', function() {
  100. connect(f);
  101. });
  102. f.on('error', function(err) { alert(err); });
  103. });
  104. // Close a connection.
  105. $('#close').click(function() {
  106. eachActiveConnection(function(c) {
  107. c.close();
  108. });
  109. });
  110. // Send a chat message to all active connections.
  111. $('#send').submit(function(e) {
  112. e.preventDefault();
  113. // For each active connection, send the message.
  114. var msg = $('#text').val();
  115. eachActiveConnection(function(c, $c) {
  116. if (c.label === 'chat') {
  117. c.send(msg);
  118. $c.find('.messages').append('<div><span class="you">You: </span>' + msg
  119. + '</div>');
  120. }
  121. });
  122. $('#text').val('');
  123. $('#text').focus();
  124. });
  125. // Goes through each active peer and calls FN on its connections.
  126. function eachActiveConnection(fn) {
  127. var actives = $('.active');
  128. actives.each(function() {
  129. var peerId = $(this).attr('id');
  130. var conns = peer.connections[peerId];
  131. var labels = Object.keys(conns);
  132. for (var i = 0, ii = labels.length; i < ii; i += 1) {
  133. var conn = conns[labels[i]];
  134. fn(conn, $(this));
  135. }
  136. });
  137. }
  138. // Show browser version
  139. $('#browsers').text(navigator.userAgent);
  140. });
  141. // Make sure things clean up properly.
  142. window.onunload = window.onbeforeunload = function(e) {
  143. if (!!peer && !peer.destroyed) {
  144. peer.destroy();
  145. }
  146. };
  147. </script>
  148. </head>
  149. <body>
  150. <a href="https://github.com/peers/peerjs"><img style="position: absolute; top: 0; right: 0; border: 0;"
  151. src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"
  152. alt="Fork me on GitHub"></a>
  153. <div id="actions">
  154. Your PeerJS ID is <span id="pid"></span><br>
  155. Connect to a peer: <input type="text" id="rid" placeholder="Someone else's id"><input class="button" type="button" value="Connect" id="connect"><br><br>
  156. <form id="send">
  157. <input type="text" id="text" placeholder="Enter message"><input class="button" type="submit" value="Send to selected peers">
  158. </form>
  159. <button id="close">Close selected connections</button>
  160. </div>
  161. <div id="wrap"><div id="connections"><span class="filler">You have not yet
  162. made any connections.</span></div>
  163. <div class="clear"></div></div>
  164. <div id="box" style="background: #fff; font-size: 18px;padding:40px 30px; text-align: center;">
  165. Drag file here to send to active connections.
  166. </div>
  167. <div class="warning browser">
  168. <div class="important">Your browser version: <span id="browsers"></span><br>
  169. Currently <strong>Firefox 22+ and Google Chrome 26.0.1403.0 or above</strong> is required.</strong></div>For more up to date compatibility
  170. information see <a href="http://peerjs.com/status">PeerJS WebRTC
  171. Status</a><br>Note that this demo may also fail if you are behind
  172. stringent firewalls or both you and the remote peer and behind symmetric
  173. NATs.
  174. <div class="log" style="color:#FF7500;text-shadow:none;padding:15px;background:#eee"><strong>Connection status</strong>:<br></div>
  175. </div>
  176. </body>
  177. </html>