olá recentemente tenho criado um sistema de vídeo chamada utilizando o peer js aqui o html:
<p id="notification" hidden></p>
<div class="entry-modal" id="entry-modal" style="opacity: 0%;">
<p>Create or Join Meeting</p>
<input id="room-input" class="room-input" placeholder="Enter Room ID" value="yCJzWAn12EZsjq2mPhPGfmRnMLXQAoLrCe8QwGLI">
<div style="display:inline-block;"><br>
<button onclick="createRoom()" class="create_room">Criar Sala</button><br><br>
<button onclick="joinRoom()" class="join_room">Entrar em uma Sala</button><br>
</div>
</div>
<video id="local-video" ></video>
<video id="remote" ></video>
e aqui o script:
const PRE = "DELTA" const SUF = "MEET" var room_id; var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; var local_stream; function createRoom(){ console.log("Creating Room") let room = document.getElementById("room-input").value; if(room == " " || room == "") { alert("Please enter room number") return; } room_id = PRE+room+SUF; let peer = new Peer(room_id) peer.on('open', (id)=>{ console.log("Peer Connected with ID: ", id) hideModal() getUserMedia({video: true, audio: true}, (stream)=>{ local_stream = stream; setLocalStream(local_stream) },(err)=>{ console.log(err) }) notify("Waiting for peer to join.") }) peer.on('call',(call)=>{ call.answer(local_stream); call.on('stream',(stream)=>{ setRemoteStream(stream) }) }) } function setLocalStream(stream){ let video = document.getElementById("local-video"); video.srcObject = stream; video.muted = true; video.play(); } function setRemoteStream(stream){ let video = document.getElementById("remote"); video.srcObject = stream; video.play(); } function hideModal(){ document.getElementById("entry-modal").hidden = true } function notify(msg){ let notification = document.getElementById("notification") notification.innerHTML = msg notification.hidden = false setTimeout(()=>{ notification.hidden = true; }, 3000) } function joinRoom(){ console.log("Joining Room") let room = document.getElementById("room-input").value; if(room == " " || room == "") { alert("Please enter room number") return; } room_id = PRE+room+SUF; hideModal() let peer = new Peer() peer.on('open', (id)=>{ console.log("Connected with Id: "+id) getUserMedia({video: true, audio: true}, (stream)=>{ local_stream = stream; setLocalStream(local_stream) notify("Joining peer") let call = peer.call(room_id, stream) call.on('stream', (stream)=>{ setRemoteStream(stream); }) }, (err)=>{ console.log(err); console.log("algo aconteceu!"); }) }) }
até ai tudo indo de boa até que eu percebi que quando um usuário fechava a transmissão ela congelava para outro usuário ai eu olhei a documentação do peer e vi que o peer js tem a função close,então adicionei ela e ficou assim:
const PRE = "DELTA" const SUF = "MEET" var room_id; var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; var local_stream; function createRoom(){ console.log("Creating Room") let room = document.getElementById("room-input").value; if(room == " " || room == "") { alert("Please enter room number") return; } room_id = PRE+room+SUF; let peer = new Peer(room_id) peer.on('open', (id)=>{ console.log("Peer Connected with ID: ", id) hideModal() getUserMedia({video: true, audio: true}, (stream)=>{ local_stream = stream; setLocalStream(local_stream) },(err)=>{ console.log(err) }) notify("Waiting for peer to join.") }) peer.on('call',(call)=>{ call.answer(local_stream); call.on('stream',(stream)=>{ setRemoteStream(stream) }) }) } function setLocalStream(stream){ let video = document.getElementById("local-video"); video.srcObject = stream; video.muted = true; video.play(); } function setRemoteStream(stream){ let video = document.getElementById("remote"); video.srcObject = stream; video.play(); } function hideModal(){ document.getElementById("entry-modal").hidden = true } function notify(msg){ let notification = document.getElementById("notification") notification.innerHTML = msg notification.hidden = false setTimeout(()=>{ notification.hidden = true; }, 3000) } function joinRoom(){ console.log("Joining Room") let room = document.getElementById("room-input").value; if(room == " " || room == "") { alert("Please enter room number") return; } room_id = PRE+room+SUF; hideModal() let peer = new Peer() peer.on('open', (id)=>{ console.log("Connected with Id: "+id) getUserMedia({video: true, audio: true}, (stream)=>{ local_stream = stream; setLocalStream(local_stream) notify("Joining peer") let call = peer.call(room_id, stream) call.on('stream', (stream)=>{ setRemoteStream(stream); }) }, (err)=>{ console.log(err); console.log("algo aconteceu!"); }) }) } let peer = new Peer();peer.on('close', function() {console.log('terminou live')});
mais ai ele não funciona,o que há de errado?