如何使用对等js终止/断开对等连接?

时间:2020-09-30 23:39:37

标签: javascript p2p peerjs

我正在进行实时申请,其中必须包含音频通话

连接,发送,接收呼叫,一切都进行得很好,但是当我尝试结束传入/传出呼叫或与之断开连接时,仅通过从函数调用call.close()就无法将其应用于当前呼叫

// cloes call
function closeCall()
{
   peer.on('call',call=>{
   call.close();
})
}

这是我应付的代码,您可以帮助实现该目标

在单击某些按钮后立即调用voiceCall()函数,我想在结束通话时也这样做

let peer = new Peer($('.current_peer_id').val(),{ debug:3 });
// sender
let target_peer_id_var;
function voiceCall(target_peer_id)
{
  target_peer_id_var = target_peer_id;
  let conn = peer.connect(target_peer_id);
  let getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
   getUserMedia({audio: true}, function(stream) {
     let call = peer.call(target_peer_id, stream);
     call.on('stream', function(remoteStream) { // declare calling now
         $('#isCallingModal').modal('show');
     });
     conn.on('open', function(){ // here you have conn.id
        conn.send([$('.current_user_name').val(),$('.current_user_img_path').attr('src')]);
    });
   }, function(err) {
     $('#isCallingModal').modal('hide');
   });
} 

peer.on('call', call => {
    // wait unitil receving data
    setTimeout(function(){
      var acceptsCall = confirm(sentBy+' is trying to call you , accept?');
      if (acceptsCall) {
          const startChat = async () => {
              const localStream = await navigator.mediaDevices.getUserMedia({
                  audio: true,
              })
              call.answer(localStream);
              call.on('stream', remoteStream => {
                  // determine calling now
                  $('#isCallingModal').modal('show');
              });
              // Handle when the call finishes
              call.on('close', function () {
                  $('#isCallingModal').modal('hide');
                  console.log('we are disconnected now !');
              });
          }
          startChat();
      } else {
          alert('call decline!');
      }
    },1000);
})

在这里,我正在传递一些数据

let details = [];
let sentBy;
// when someone is calling
peer.on('connection', function(conn) {
  conn.on('data', function(data){
    details.push(data[0],data[1]);
    $('#isCallingImgPath').attr('src',data[1]);
    $('#isCallingName').html(data[0]);
    sentBy = data[0];
  });
});

0 个答案:

没有答案
相关问题