addicecandidate 应该添加空字符串 icecandidate 吗?

Should empty string icecandidate be added by addicecandidate?

我的问题是关于 webrtc 协商的。

很多网上的教程和MDN中描述的有矛盾。

在 MDN 中,它表示 link

At the end of each generation of candidates, an end-of-candidates notification is sent in the form of an RTCIceCandidate whose candidate property is an empty string. This candidate should still be added to the connection using addIceCandidate() method, as usual, in order to deliver that notification to the remote peer.

When there are no more candidates at all to be expected during the current negotiation exchange, an end-of-candidates notification is sent by delivering a RTCIceCandidate whose candidate property is null. This message does not need to be sent to the remote peer. It's a legacy notification of a state which can be detected instead by watching for the iceGatheringState to change to complete, by watching for the icegatheringstatechange event.

然而,在教程here中,他们引入了以下代码

function handleICECandidateEvent(event) {
  if (event.candidate) {
    sendToServer({
      type: "new-ice-candidate",
      target: targetUsername,
      candidate: event.candidate
    });
  }
}

如果 candidate 是一个空字符串,它将被评估为 false 并且不会通过 sendToServer 发送。

更有趣的是,即使在同一篇文章中here

他们有以下示例代码

rtcPeerConnection.onicecandidate = (event) => {
  if (event.candidate) {
    sendCandidateToRemotePeer(event.candidate)
  }
}

但就在这段代码的下方,他们说

When an ICE negotiation session runs out of candidates to propose for a given RTCIceTransport, it has completed gathering for a generation of candidates. That this has occurred is indicated by an icecandidate event whose candidate string is empty ("").

You should deliver this to the remote peer just like any standard candidate, as described under Sharing a new candidate above. This ensures that the remote peer is given the end-of-candidates notification as well.

其实我看了很多在线教程,但是我从来没有见过他们处理空字符串候选的地方。

旧规范不要求发送空候选,但新规范要求发送和 addIceCandidate() 空候选。 由于Chrome还是老规范,空的candidate在addedIceCandidate()时会报错,所以就不发了。