瀏覽代碼

hello world simple example

Michelle Bu 12 年之前
父節點
當前提交
97c209b412
共有 1 個文件被更改,包括 62 次插入0 次删除
  1. 62 0
      examples/helloworld.html

+ 62 - 0
examples/helloworld.html

@@ -0,0 +1,62 @@
+<!DOCTYPE HTML> 
+<html lang="en"> 
+<head>
+<title>PeerJS Hello World Code Example</title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
+<meta http-equiv="Content-Language" content="en-us"> 
+
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
+<script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script>
+<script>
+  // This is a very simple code example. See chat.html for a more involved
+  // example.
+
+  $(document).ready(function() {
+    var peer1, peer2, peerId1;
+
+    // Create a new Peer with our demo API key, with debug set to true so we can
+    // see what's going on.
+    peer1 = new Peer({ key: 'lwjd5qra8257b9', debug: true });
+
+    // The `open` event signifies that the Peer is ready to connect with other
+    // Peers and, if we didn't provide the Peer with an ID, that an ID has been
+    // assigned by the server.
+    peer1.on('open', function(id){
+      peerId1 = id;
+
+      // Create another Peer with our demo API key to connect to.
+      peer2 = new Peer({ key: 'lwjd5qra8257b9', debug: true });
+
+      // Connect to the first peer.
+      var connection = peer2.connect(peerId1);
+      connection.on('data', function(data) {
+        // When we receive 'Hello', send ' world'.
+        $('body').append(data);
+        connection.send(' world');
+      });
+    });
+
+    // Wait for a connection from the second peer.
+    peer1.on('connection', function(connection) {
+      // This `connection` is a DataConnection object with which we can send
+      // data.
+      // The `open` event firing means that the connection is now ready to
+      // transmit data.
+      connection.on('open', function() {
+        // Send 'Hello' on the connection.
+        connection.send('Hello');
+      });
+      // The `data` event is fired when data is received on the connection.
+      connection.on('data', function(data) {
+        // Append the data to body.
+        $('body').append(data);
+      });
+    });
+  });
+
+</script>
+</head> 
+ 
+<body> 
+</body> 
+</html>