helloworld.html 2.1 KB

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