helloworld.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  8. <!--script type="text/javascript"
  9. src="http://cdn.peerjs.com/0/peer.js"></script-->
  10. <script type="text/javascript" src="../dist/peer.js"></script>
  11. <script>
  12. // This is a very simple code example. See chat.html for a more involved
  13. // example.
  14. $(document).ready(function() {
  15. var peer1, peer2, peerId1;
  16. // Create a new Peer with our demo API key, with debug set to true so we can
  17. // see what's going on.
  18. peer1 = new Peer({ key: 'lwjd5qra8257b9', debug: true });
  19. // Create another Peer with our demo API key to connect to.
  20. peer2 = new Peer({ key: 'lwjd5qra8257b9', debug: true });
  21. // The `open` event signifies that the Peer is ready to connect with other
  22. // Peers and, if we didn't provide the Peer with an ID, that an ID has been
  23. // assigned by the server.
  24. peer1.on('open', function(id){
  25. peerId1 = id;
  26. var c = peer2.connect(peerId1);
  27. c.on('data', function(data) {
  28. // When we receive 'Hello', send ' world'.
  29. $('body').append(data);
  30. c.send(' world');
  31. });
  32. });
  33. // Wait for a connection from the second peer.
  34. peer1.on('connection', function(connection) {
  35. // This `connection` is a DataConnection object with which we can send
  36. // data.
  37. // The `open` event firing means that the connection is now ready to
  38. // transmit data.
  39. connection.on('open', function() {
  40. // Send 'Hello' on the connection.
  41. connection.send('Hello');
  42. });
  43. // The `data` event is fired when data is received on the connection.
  44. connection.on('data', function(data) {
  45. // Append the data to body.
  46. $('body').append(data);
  47. });
  48. });
  49. });
  50. </script>
  51. </head>
  52. <body>
  53. </body>
  54. </html>