PeerJS使用帮助
官方网址:http://peerjs.com
GitHub:https://github.com/peers/peerjs/
简介描述:一个完整、可配置、易于使用、基于WebRTC的点对点API
PeerJS 提供了一个完整、可配置、易于使用、基于WebRTC的点对点的数据通信。PeerJS是一个开源的JavaScript库,目的是允许运行在不同系统上的Web应用程序相互联系。PeerJS开发者称,PeerJS完善了WebRTC,因为作为视频连接协议,WebRTC并没有说明基于WebRTC的客户端应该如何定位连接的用户。
PeerJS有一个简单的API,允许通过三行代码来实现对等连接。PeerServer作为PeerJS的后端,这是一个基于Node.js的web服务器,这也是开源的。
Setup
Include the library
with npm: npm install peerjs and the usage:
import Peer from 'peerjs';
Create a Peer
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.
Data connections
Connect
const conn = peer.connect('another-peers-id'); conn.on('open', () => { conn.send('hi!'); });
Receive
peer.on('connection', (conn) => { conn.on('data', (data) => { // Will print 'hi!' console.log(data); }); });