我正在遵循这篇教程,制作一个简单的WebRTC示例。但是远程视频在任何浏览器中都不会出现,并且Chrome没有显示错误信息:
所有字体: 代码取自此 Github 存储库。
我在not setRemoteDescription方法中进行了日志记录:Uncaught (in promise) DOMException: Error processing ICE candidate
peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp), function(){
alert('success')
}, function(e){ console.log(e); alert(e)});
然后我遇到了以下错误:
操作错误:无法设置远程offer的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 存储库。
answerCall()
函数是用来回应呼叫的。请注意,在调用createAnswer()
之前,必须先调用setRemoteDescription(offer)
。因为offer
是回应的基础。 - jib