关闭RTCPeerConnection后,我无法再次拨打电话,因为未触发onicecandidate

问题描述

我想使用webrtc建立对等连接。当我第一次创建RTCPeerConnection时,它工作正常,但是当我使用close()方法关闭连接并尝试通过创建新的RTCPeerConnection再次调用时,未建立调用,因为未触发onicecandidate事件,因此无法将候选冰发送到信号器。

    var config = {
        apiKey: "AIzaSyB_hC6EAi4Lcubx5oKdt6XhfUhy9eIqt3A",authDomain: "maqraa-18b00.firebaseapp.com",databaseURL: "https://maqraa-18b00.firebaseio.com",projectId: "maqraa-18b00",storageBucket: "maqraa-18b00.appspot.com",messagingSenderId: "484319770328",appId: "1:484319770328:web:d814f4b0787ebcdca709ad",measurementId: "G-4M23TN8TNL"
    };
    firebase.initializeApp(config);
    OpenMyMic();
    
    const callButton = document.querySelector('button#callButton');
    const hangupButton = document.querySelector('button#hangupButton');
    var partnerAudio = document.querySelector('.audio.partner');
    callButton.disabled = false;
    hangupButton.disabled = true;   
    callButton.onclick = call;
    hangupButton.onclick = hangup;
    
    
    var database = firebase.database().ref();
    var yourId = Math.floor(Math.random()*1000000000);
    var servers = {iceServers: [{
       urls: [ "stun:bn-turn1.xirsys.com" ]
    },{
       username: "4YZ55YVDuwcyQ9eBVVfFPUIXleT0R_Q-V-MmSugmX0NqHNaPrgNaRtY3dJXq51WzAAAAAF-eg9Bpc2FsZWg=",credential: "07d3299e-1c27-11eb-8f9c-0242ac140004",urls: [
           "turn:bn-turn1.xirsys.com:80?transport=udp","turn:bn-turn1.xirsys.com:3478?transport=udp","turn:bn-turn1.xirsys.com:80?transport=tcp","turn:bn-turn1.xirsys.com:3478?transport=tcp","turns:bn-turn1.xirsys.com:443?transport=tcp","turns:bn-turn1.xirsys.com:5349?transport=tcp"
       ]
    }]};
    let pc;
    createPC();
    function createPC()
    {
    debugger
    pc = new RTCPeerConnection(servers);
    pc.onicecandidate = (event => event.candidate?sendMessage(yourId,JSON.stringify({'ice': event.candidate})):console.log("Sent All Ice") );
    pc.onaddstream = (event => partnerAudio.srcObject = event.stream);
    }
    function sendMessage(senderId,data) {
        debugger
        var msg = database.push({ sender: senderId,message: data });
        msg.remove();
    }
    var messageRead = false;
    function readMessage(data) {
        
        
       
        var msg = JSON.parse(data.val().message);
        var sender = data.val().sender;
        if (sender != yourId) {
                
            if(msg.info == 'hangup')
            {
                debugger
              callButton.disabled = false;
              hangupButton.disabled = true; 
              pc.close();
            }
            else if(msg.info == 'call')
            {
              callButton.disabled = true;
              hangupButton.disabled = false;    
              if(pc.connectionState == "closed")
                {debugger
                 createPC();    
                }
            }
            else if (msg.ice != undefined)
                pc.addIceCandidate(new RTCIceCandidate(msg.ice));
            else if (msg.sdp.type == "offer")
                pc.setRemoteDescription(new RTCSessionDescription(msg.sdp))
                  .then(() => pc.createAnswer())
                  .then(answer => pc.setLocalDescription(answer))
                  .then(() => sendMessage(yourId,JSON.stringify({'sdp': pc.localDescription})));
            else if (msg.sdp.type == "answer")
                pc.setRemoteDescription(new RTCSessionDescription(msg.sdp));
        }
    };
    
    database.on('child_added',readMessage);
    
    function OpenMyMic() {
      navigator.mediaDevices.getUserMedia({audio:true})
        .then(stream => pc.addStream(stream));
    }
    
    function call() {
        
     if(pc.connectionState == "closed")
         {debugger
          createPC();   
     }
        console.log('call started');
        pc.createOffer()
        .then(offer => pc.setLocalDescription(offer) )
        .then(() => sendMessage(yourId,JSON.stringify({'sdp': pc.localDescription})) );
        
        callButton.disabled = true;
        hangupButton.disabled = false;
        
        sendMessage(yourId,JSON.stringify({'info': 'call'})) 
    }
    
     function hangup() {     
           pc.close();    
           callButton.disabled = false;
           hangupButton.disabled = true;    
           sendMessage(yourId,JSON.stringify({'info': 'hangup'}))     
     }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)