多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 概述 发送方 ``` <input type="text" name="peer_id"><button id="peer_id"> 提交</button> <div style="width: 600px;height: 600px"> <video id="lcoalvideo" src=""></video> </div> <script src="https://unpkg.com/peerjs@1.3.2/dist/peerjs.min.js"></script> <script> var otherPeerIdBtn=document.querySelector("#peer_id") var otherPeerIdInput=document.querySelector("[name='peer_id']") var peer = new Peer(null,{debug:2}); peer.on('open', function(id) { console.log('My peer ID is: ' + id); }); otherPeerIdBtn.addEventListener("click",connect) function connect(event){ console.log("trigger"); var peerid =otherPeerIdInput.value var conn = peer.connect(peerid); conn.on('open', function() { // Send messages console.log("send hello"); conn.send('Hello!!!!!'); }); conn.on("data",function(data){ console.log("rece "+data); }) } </script> ``` 接受方 ``` <p id="peer_id"></p> <script src="https://unpkg.com/peerjs@1.3.2/dist/peerjs.min.js"></script> <script> var peer = new Peer(null,{debug:2}); var peerid=document.querySelector("#peer_id") peer.on('open', function(id) { peerid.innerHTML=id; console.log('My peer ID is: ' + id); }); peer.on('connection', function(conn) { conn.on('data', function(data) { console.log('Received', data); conn.send('world!!!'); }); }); peer.on('call', function(call) { }); </script> ```