企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[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> ```