chat.html 5.7 KB

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