关闭WebRTC连接并重新连接

时间:2019-06-26 12:00:00

标签: javascript webrtc p2p ice rtcpeerconnection

我在一个webrtc视频通话站点上工作,到目前为止一切正常,包括关闭对等连接都很好,但是问题是当我关闭对等然后又尝试重新对等时,这是行不通的而且我遇到了一些未捕获的错误,现在已经有3天了。到目前为止,这是我的代码:

 let pc = new RTCPeerConnection(servers);

 pc.onicecandidate = (event => event.candidate ?
  sendMessage(yourId, callId, JSON.stringify({'ice': event.candidate
  })) : console.log("Sent All Ice"));
  pc.onaddstream = (event => friendsVideo.srcObject = event.stream);

 function showFriendsFace() {
 pc.createOffer()
 .then(offer => pc.setLocalDescription(offer))
 .then(() => sendMessage(yourId, callId, JSON.stringify({
  'sdp': pc.localDescription
  })));

 return false;
 }

function readMessage(data) {
 let msg = JSON.parse(data.message);
 let sender = data.sender;
 let receiver = data.receiver;

 console.log(receiver);
 console.log("Message from " + sender + " to " + receiver);

if (receiver === yourId) {
 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, sender, JSON.stringify({
      'sdp': pc.localDescription
    })));

    console.log("offer")
  }else if (msg.sdp.type === "answer"){
   pc.setRemoteDescription(new RTCSessionDescription(msg.sdp));
   console.log("answer")
  }
}
}

// To close peer
endBtn.addEventListener('click', function () {
 pc.mediaHandler = null;
 pc.onicecandidate = null; 
 pc.onaddstream = null; 
 pc.localDescription = null;

 pc.close();
 pc = null;
});

我收到未捕获的错误,例如:setRemoteDescription,createOffer

感谢您的帮助。

0 个答案:

没有答案