瀏覽代碼

Update README.md

Eric Zhang 11 年之前
父節點
當前提交
257f5ca594
共有 1 個文件被更改,包括 26 次插入38 次删除
  1. 26 38
      README.md

+ 26 - 38
README.md

@@ -18,17 +18,18 @@ PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built
 var peer = new Peer('pick-an-id', {key: 'myapikey'}); 
 // You can pick your own id or omit the id if you want to get a random one from the server.
 ```
+Get a [free API key](http://peerjs.com/peerserver)
 
 ## Data connections
+**Connect**
 ```javascript
-
-// Connect
-var conn = peer.connect('a-peers-id');
+var conn = peer.connect('another-peers-id');
 conn.on('open', function(){
   conn.send('hi!');
 });
-
-// Receive
+```
+**Receive**
+```javascript
 peer.on('connection', function(conn) {
   conn.on('data', function(data){
     // Will print 'hi!'
@@ -37,52 +38,39 @@ peer.on('connection', function(conn) {
 });
 ```
 
-**Media calls**
+## Media calls
+**Call**
 ```javascript
 var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
-peer.on('call', function(call) {
-  getUserMedia({video: true, audio: true}, function(stream) {
-    call.on('stream', function(stream) {
-      // Show stream in some <video> element.
-    });
-    call.answer(stream); // Answer any call with an A/V stream.
-  }, function(err) {
-    console.log(err);
+getUserMedia({video: true, audio: true}, function(stream) {
+  var call = peer.call('another-peers-id', stream);
+  call.on('stream', function(remoteStream) {
+    // Show stream in some <video> element.
   });
+}, function(err) {
+  console.log('Failed to get local stream' ,err);
 });
-```
 
 ```
-
-**Connecting peer**
-
+**Answer**
 ```javascript
-var peer = new Peer('thing2', {key: 'myapikey'}); // You can omit the ID if you want to get a random one from the server.
-
-/** Data connections. */
-
-var connection = peer.connect('thing1');
-connection.on('open', function() {
-  connection.send('hi!'); // Send 'hi!' when the data connection opens.
-});
-
-/** Media calls. */
-
 var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
-getUserMedia({video: true, audio: true}, function(stream) {
-  var call = peer.call('thing1', stream);
-  call.on('stream', function(stream) {
-    // Show stream in some <video> element.
+peer.on('call', function(call) {
+  getUserMedia({video: true, audio: true}, function(stream) {
+    call.answer(stream); // Answer the call with an A/V stream.
+    call.on('stream', function(remoteStream) {
+      // Show stream in some <video> element.
+    });
+  }, function(err) {
+    console.log('Failed to get local stream' ,err);
   });
-}, function(err) {
-  console.log(err);
 });
-
 ```
+## Links
 
-### [Documentation](http://peerjs.com/docs)
+### [Documentation / API Reference](http://peerjs.com/docs)
 
-### [Browser compatibility status](http://peerjs.com/status)
+### [WebRTC Browser compatibility status](http://peerjs.com/status)
 
 ### [PeerServer](https://github.com/peers/peerjs-server)