|
@@ -5,7 +5,7 @@
|
|
|
|
|
|
PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC, supporting both data channels and media streams.
|
|
PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC, supporting both data channels and media streams.
|
|
|
|
|
|
-### [http://peerjs.com](http://peerjs.com)
|
|
|
|
|
|
+### [https://peerjs.com](https://peerjs.com)
|
|
|
|
|
|
## Setup
|
|
## Setup
|
|
|
|
|
|
@@ -21,24 +21,23 @@ PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built
|
|
|
|
|
|
|
|
|
|
**Create a Peer**
|
|
**Create a Peer**
|
|
-Get a [free API key](http://peerjs.com/peerserver). Your id only needs to be unique to the namespace of your API key.
|
|
|
|
```javascript
|
|
```javascript
|
|
-var peer = new Peer('pick-an-id', {key: 'myapikey'});
|
|
|
|
|
|
+const peer = new Peer('pick-an-id');
|
|
// You can pick your own id or omit the id if you want to get a random one from the server.
|
|
// You can pick your own id or omit the id if you want to get a random one from the server.
|
|
```
|
|
```
|
|
|
|
|
|
## Data connections
|
|
## Data connections
|
|
**Connect**
|
|
**Connect**
|
|
```javascript
|
|
```javascript
|
|
-var conn = peer.connect('another-peers-id');
|
|
|
|
-conn.on('open', function(){
|
|
|
|
|
|
+const conn = peer.connect('another-peers-id');
|
|
|
|
+conn.on('open', () => {
|
|
conn.send('hi!');
|
|
conn.send('hi!');
|
|
});
|
|
});
|
|
```
|
|
```
|
|
**Receive**
|
|
**Receive**
|
|
```javascript
|
|
```javascript
|
|
-peer.on('connection', function(conn) {
|
|
|
|
- conn.on('data', function(data){
|
|
|
|
|
|
+peer.on('connection', (conn) => {
|
|
|
|
+ conn.on('data', (data) => {
|
|
// Will print 'hi!'
|
|
// Will print 'hi!'
|
|
console.log(data);
|
|
console.log(data);
|
|
});
|
|
});
|
|
@@ -49,40 +48,40 @@ peer.on('connection', function(conn) {
|
|
**Call**
|
|
**Call**
|
|
```javascript
|
|
```javascript
|
|
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
-navigator.getUserMedia({video: true, audio: true}, function(stream) {
|
|
|
|
- var call = peer.call('another-peers-id', stream);
|
|
|
|
- call.on('stream', function(remoteStream) {
|
|
|
|
|
|
+navigator.getUserMedia({video: true, audio: true}, (stream) => {
|
|
|
|
+ const call = peer.call('another-peers-id', stream);
|
|
|
|
+ call.on('stream', (remoteStream) => {
|
|
// Show stream in some <video> element.
|
|
// Show stream in some <video> element.
|
|
});
|
|
});
|
|
-}, function(err) {
|
|
|
|
- console.log('Failed to get local stream' ,err);
|
|
|
|
|
|
+}, (err) => {
|
|
|
|
+ console.error('Failed to get local stream', err);
|
|
});
|
|
});
|
|
|
|
|
|
```
|
|
```
|
|
**Answer**
|
|
**Answer**
|
|
```javascript
|
|
```javascript
|
|
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
-peer.on('call', function(call) {
|
|
|
|
- navigator.getUserMedia({video: true, audio: true}, function(stream) {
|
|
|
|
|
|
+peer.on('call', (call) => {
|
|
|
|
+ navigator.getUserMedia({video: true, audio: true}, (stream) => {
|
|
call.answer(stream); // Answer the call with an A/V stream.
|
|
call.answer(stream); // Answer the call with an A/V stream.
|
|
- call.on('stream', function(remoteStream) {
|
|
|
|
|
|
+ call.on('stream', (remoteStream) => {
|
|
// Show stream in some <video> element.
|
|
// Show stream in some <video> element.
|
|
});
|
|
});
|
|
- }, function(err) {
|
|
|
|
- console.log('Failed to get local stream' ,err);
|
|
|
|
|
|
+ }, (err) => {
|
|
|
|
+ console.error('Failed to get local stream', err);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
```
|
|
```
|
|
|
|
|
|
-## Run tests
|
|
|
|
|
|
+## Running tests
|
|
|
|
|
|
-`npm test`
|
|
|
|
|
|
+```bash
|
|
|
|
+npm test
|
|
|
|
+```
|
|
|
|
|
|
## Links
|
|
## Links
|
|
|
|
|
|
-### [Documentation / API Reference](http://peerjs.com/docs)
|
|
|
|
-
|
|
|
|
-### [WebRTC Browser compatibility status](http://peerjs.com/status)
|
|
|
|
|
|
+### [Documentation / API Reference](https://peerjs.com/docs)
|
|
|
|
|
|
### [PeerServer](https://github.com/peers/peerjs-server)
|
|
### [PeerServer](https://github.com/peers/peerjs-server)
|
|
|
|
|