本文介绍了WebRTC:在错误状态下调用:STATE_SENTOFFER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 本教程,做一个简单的WebRTC例子.但是远程视频没有出现在任何浏览器中,Chrome也没有显示错误:

未捕获(承诺)DOMException:处理 ICE 候选对象时出错

我做了一个没有setRemoteDescription方法的日志:

peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp), function(){警报('成功')}, function(e){ console.log(e);警报(e)});

然后我收到以下错误:

OperationError:无法设置远程报价 sdp:在错误状态下调用:STATE_SENTOFFER

在有问题的教程中,作者声称他能够正确地做所有事情,错误应该在我这边.有没有人遇到过这种情况?

(对不起我的英语)

(包括代码)

我仍然是这个主题的外行,开头引用的教程链接是我发现开始玩乐的最干净的链接.我会放出我认为重要的来源:

后端 - server.js

/** 连接成功 */wss.on('connection', function (client) {console.log("连接了一个新的 WebSocket 客户端.");/** 传入消息 */client.on('message', function (message) {/** 向所有客户端广播消息 */wss.broadcast(消息,客户端);});});//向所有 WebSocket 客户端广播消息.wss.broadcast = 函数(数据,排除){var i = 0, n = this.clients ?this.clients.length : 0, client = null;如果 (n 

前端 - webrtc.js

wsc.onmessage = function (evt) {var 信号 = 空;if (!peerConn) answerCall();信号 = JSON.parse(evt.data);如果(信号.sdp){console.log("从远程对端接收到 SDP.");peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp),功能(){},功能(e){ console.log(e);});}否则如果(信号.候选){console.log("收到来自远程对等方的 ICECandidate.");peerConn.addIceCandidate(new RTCIceCandidate(signal.candidate));} else if (signal.closeConnection){console.log("收到来自远程对等方的'关闭呼叫'信号.");结束通话();}};

所有字体:代码来自这个 github 存储库.

解决方案

不看代码很难回答,但从两个错误来看,你至少有两个问题:

未捕获(承诺)DOMException:处理 ICE 候选对象时出错

这是来自 peerConn.addIceCandidate(candidate) 并且候选输入有问题,表明它不正确或以某种方式被破坏.你应该通过你的信号通道从对方的 peerConn.onicecandidate 发出信号.如果需要更多帮助,请显示代码.

它是未捕获的",因为它返回了一个承诺,而你缺少一个 .catch:

peerConn.addIceCandidate(candidate).catch(e => console.log(e));

OperationError: Failed to set remote offer sdp: Called in wrong state: STATE_SENTOFFER

这表明两个对等方都试图同时发送报价,这是对称且错误的.

要约/答案交换本质上是不对称的.一方必须从一个提议开始,另一方接收它,执行 SetRemote,createAnswer,并将回答发送回第一个执行 setRemote 的对等方.这种舞蹈是一个状态机.任何失误都会出现这样的错误.

I'm following this tutorial, to make a simple example of WebRTC. But the remote video does not appear in any browser and Chrome does not show the error:

I made a log not setRemoteDescription method:

peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp), function(){
       alert('success')
    }, function(e){ console.log(e); alert(e)});

And then I get the following error:

In the tutorial in question, the author claims that he is able to do everything right and that the error should be on my side. Has anyone ever experienced this?

(sorry about my English)


EDIT: (Include code)

I am still a layman on the subject, the tutorial link quoted at the beginning was the cleanest I found to start having fun. I'll put the source I think it's important:

Back-end - 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);
  }
};

Front-end - 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();
  }
};

All font: Code taken from this github repository.

解决方案

Hard to answer without seeing code, but you have at least two problems, judging by the two errors:

This is from peerConn.addIceCandidate(candidate) and there's something wrong with the candidate input, suggesting it's incorrect or mangled somehow. You're supposed to signal it over your signaling channel from the other side's peerConn.onicecandidate. Show code please if more help is needed.

It's "uncaught" because it returns a promise, and you're missing a .catch:

peerConn.addIceCandidate(candidate).catch(e => console.log(e));

This suggests both peers tried to send an offer at the same time, which is symmetric and wrong.

The offer/answer exchange is inherently asymmetric. One side must start with an offer, the other side receives it, does SetRemote, createAnswer, and sends answer back to the first peer, which does setRemote. This dance is a state-machine. Any misstep and you get an error like this.

这篇关于WebRTC:在错误状态下调用:STATE_SENTOFFER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 22:25