PeerJS
PeerJS 介绍
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);
});
});
GitHub:https://github.com/peers/peerjs/
网站描述:一个完整、可配置、易于使用、基于WebRTC的点对点API
PeerJS官方网站
官方网站:http://peerjs.com