我得到这个错误在我的catch块,当我做myPeerConnection.createAnswer()
PeerConnection不能在具有have-remote-off或have-local-pranswer之外的状态下创建答案。
我正在使用socket.io作为信令服务器。我正在遵循MDN的教程
这是我的代码:
myPeerConnection.setRemoteDescription(desc).then(() => {
return navigator.mediaDevices.getUserMedia(mediaConstraints);
}).then((stream) => {
localStream = stream;
document.getElementById("localVideo").srcObject = localStream;
return myPeerConnection.addStream(localStream);
}).then(() => {
return myPeerConnection.createAnswer(); //No error when removed this then chain
}).then((answer) => {
return myPeerConnection.setLocalDescription(answer); // No error when removed this then chain
}).then(() => {
socket.emit('video-answer', {
sdp: myPeerConnection.localDescription
});
}).catch(handleGetUserMediaError);
Run Code Online (Sandbox Code Playgroud)
这里的答案也没有帮助我。
我已将整个项目上传到Github。您可以在此处查看脚本文件。
任何帮助表示赞赏。
我希望逻辑上没有缺陷。
步骤1:呼叫者创建报价
步骤2:呼叫者设置localDescription
步骤3:呼叫者将描述发送给被呼叫者
// ------------------------------------------------ ------ //
步骤4:被叫方收到要约集的远程描述
步骤5:被叫方创建答案
步骤6:被叫方设置本地描述
步骤7:被呼叫者将说明发送给呼叫者
// ------------------------------------------------ ------ //
步骤8:呼叫者收到答案并设置远程描述
这是上面的代码
const socket = io();
const constraints = {
audio: true,
video: true
};
const configuration = {
iceServers: [{
"url": "stun:23.21.150.121"
}, {
"url": "stun:stun.l.google.com:19302"
}]
};
const selfView = $('#selfView')[0];
const remoteView = $('#remoteView')[0];
var pc = new RTCPeerConnection(configuration);
pc.onicecandidate = ({
candidate
}) => {
socket.emit('message', {
to: $('#remote').val(),
candidate: candidate
}); …Run Code Online (Sandbox Code Playgroud)