chat.html 7.1 KB

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