chat.html 6.2 KB

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