我正在按照本教程做一个简单的 WebRTC 示例。但是远程视频没有出现在任何浏览器中,Chrome 也没有显示错误:
Uncaught (in promise) DOMException: Error processing ICE Candidate
我做了一个 log not setRemoteDescription 方法:
peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp), function(){
alert('success')
}, function(e){ console.log(e); alert(e)});
然后我收到以下错误:
OperationError:无法设置远程报价 sdp:在错误状态下调用:STATE_SENTOFFER
在有问题的教程中,作者声称他能够正确地做所有事情,并且错误应该在我这边。有没有人经历过这个?
(对不起我的英语)
编辑:(包括代码)
我仍然是这个主题的外行,开头引用的教程链接是我发现开始玩得最干净的。我将把我认为很重要的来源:
后端 - server.js
/** successful connection */
wss.on('connection', function (client) {
console.log("A new WebSocket client was connected.");
/** incomming message */
client.on('message', function (message) {
/** broadcast message to all clients */
wss.broadcast(message, client);
});
});
// broadcasting the message to all WebSocket clients.
wss.broadcast = function (data, exclude) {
var i = 0, n = this.clients ? this.clients.length : 0, client = null;
if (n < 1) return;
console.log("Broadcasting message to all " + n + " WebSocket clients.");
for (; i < n; i++) {
client = this.clients[i];
// don't send the message to the sender...
if (client === exclude) continue;
if (client.readyState === client.OPEN) client.send(data);
else console.error('Error: the client state is ' + client.readyState);
}
};
前端 - webrtc.js
wsc.onmessage = function (evt) {
var signal = null;
if (!peerConn) answerCall();
signal = JSON.parse(evt.data);
if (signal.sdp) {
console.log("Received SDP from remote peer.");
peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp),
function(){},
function(e){ console.log(e);
});
}
else if (signal.candidate) {
console.log("Received ICECandidate from remote peer.");
peerConn.addIceCandidate(new RTCIceCandidate(signal.candidate));
} else if ( signal.closeConnection){
console.log("Received 'close call' signal from remote peer.");
endCall();
}
};
所有字体:取自此 github 存储库的代码。