helloworld.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>PeerJS Hello World Code Example</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="Content-Language" content="en-us">
  7. <script>
  8. // Just for demo.
  9. console._log = console.log;
  10. console.error = console.log = function() {
  11. var copy = Array.prototype.slice.call(arguments).join(' ');
  12. $('.log').append(copy + '<br>');
  13. console._log(copy);
  14. };
  15. </script>
  16. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  17. <script type="text/javascript" src="../dist/peer.js"></script>
  18. <script>
  19. // This is a very simple code example. See chat.html for a more involved
  20. // example.
  21. $(document).ready(function() {
  22. var peer1, peer2, peerId1;
  23. // Create a new Peer with our demo API key, with debug set to true so we can
  24. // see what's going on.
  25. peer1 = new Peer({ key: 'lwjd5qra8257b9', debug: true });
  26. // Create another Peer with our demo API key to connect to.
  27. peer2 = new Peer({ key: 'lwjd5qra8257b9', debug: true });
  28. // The `open` event signifies that the Peer is ready to connect with other
  29. // Peers and, if we didn't provide the Peer with an ID, that an ID has been
  30. // assigned by the server.
  31. peer1.on('open', function(id){
  32. peerId1 = id;
  33. var c = peer2.connect(peerId1);
  34. c.on('data', function(data) {
  35. // When we receive 'Hello', send ' world'.
  36. $('#helloworld').append(data);
  37. c.send(' peer');
  38. });
  39. });
  40. // Wait for a connection from the second peer.
  41. peer1.on('connection', function(connection) {
  42. // This `connection` is a DataConnection object with which we can send
  43. // data.
  44. // The `open` event firing means that the connection is now ready to
  45. // transmit data.
  46. connection.on('open', function() {
  47. // Send 'Hello' on the connection.
  48. connection.send('Hello,');
  49. });
  50. // The `data` event is fired when data is received on the connection.
  51. connection.on('data', function(data) {
  52. // Append the data to body.
  53. $('#helloworld').append(data);
  54. });
  55. });
  56. // Show browser version
  57. $('#browsers').text(navigator.userAgent);
  58. });
  59. </script>
  60. <style>
  61. #helloworld {
  62. font-weight: 600;
  63. font-size: 30px;
  64. padding: 20px;
  65. background-color: #4dace2;
  66. border: 1px solid #0C6BA1;
  67. max-width: 600px;
  68. }
  69. #browsers {
  70. font-weight: 600;
  71. }
  72. .warning {
  73. max-width: 600px;
  74. padding: 20px;
  75. background-color: #eee;
  76. border: 1px solid #ccc;
  77. font-size: 18px;
  78. }
  79. .browserinfo {
  80. padding: 20px;
  81. border: 1px solid #ccc;
  82. background-color: #f8f8f8;
  83. }
  84. a {
  85. font-weight: 600;
  86. }
  87. </style>
  88. </head>
  89. <body>
  90. <a href="https://github.com/peers/peerjs"><img style="position: absolute; top: 0; right: 0; border: 0;"
  91. src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"
  92. alt="Fork me on GitHub"></a>
  93. <div id="helloworld"></div>
  94. <div class="warning browser"><div class="important">
  95. Good news! If you can see the text in the blue box above, your Chrome is up to
  96. date (version 26) and you can now use WebRTC P2P
  97. DataChannels.
  98. <br>
  99. Open up your Chrome inspector to see what's going on under the hood.
  100. <br><br>
  101. Not cool enough? Try out <a
  102. href="http://cdn.peerjs.com/demo/chat.html">a chat demo</a>
  103. with a friend.
  104. <br>
  105. This demo was built with <a href="http://peerjs.com">PeerJS.</a><br><br>
  106. <div class="browserinfo">
  107. Your browser version: <span id="browsers"></span><br>
  108. Currently <strong>Firefox 22+ and Google Chrome 26.0.1403.0 or above</strong> is
  109. required.</strong></div><br>For more up to date compatibility
  110. information see <a href="http://peerjs.com/status">PeerJS WebRTC
  111. Status</a><br>Note that this demo may also fail if you are behind
  112. stringent firewalls.</div></div>
  113. <div class="log" style="color:#FF7500;text-shadow:none;padding:15px;"><strong>Connection status</strong>:<br></div>
  114. </body>
  115. </html>