chat.html 5.5 KB

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