ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] > [home](https://peerjs.com/) > [github-peers](https://github.com/peers/peerjs) > [github-peerjs-server](https://github.com/peers/peerjs-server) ## 概述 Peer.js 做了底层的 API 调用以及兼容,简化了整个端对端实现过程。 ## 快速入门 ### 设置 Include the library ``` <script src="https://unpkg.com/peerjs@1.4.7/dist/peerjs.min.js"></script> ``` Create a peer ``` var peer = new Peer(); // or var peer = new Peer({ config: {'iceServers': [ { url: 'stun:stun.l.google.com:19302' }, { url: 'turn:homeo@turn.bistri.com:80', credential: 'homeo' } ]} /* Sample servers, please use appropriate ones */ }); ``` ### 连接 Connect ``` var conn = peer.connect('another-peers-id'); // on open will be launch when you successfully connect to PeerServer conn.on('open', function(){ // here you have conn.id conn.send('hi!'); }); ``` Receive ``` peer.on('connection', function(conn) { conn.on('data', function(data){ // Will print 'hi!' console.log(data); }); }); ``` ### 媒体传输 Call ``` var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 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/canvas element. }); }, function(err) { console.log('Failed to get local stream' ,err); }); ``` Answer ``` var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 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/canvas element. }); }, function(err) { console.log('Failed to get local stream' ,err); }); }); ``` ### 服务端 PeerServer > [github-peerjs-server](https://github.com/peers/peerjs-server)