RTCIceCandidate: relatedPort 属性

Baseline 2024
新推出

自 2024 年 5 月以来,此功能已在最新设备和浏览器版本中可用。此功能可能不适用于较旧的设备或浏览器。

RTCIceCandidate 接口的只读属性 relatedPort 指示了反射(reflexive)或中继(relay)候选者的端口号。

如果该候选者是主机(host)候选者(即,其 address 实际上是远程对等方的真实 IP 地址),则 relatedPortnull

relatedPort 字段的值是从传递给 RTCIceCandidate() 构造函数的 candidateInfo options 对象设置的。你不能直接在 options 对象中指定 relatedPort 的值,但如果该对象的 candidate a-line 格式正确(rel-port 字段),它的值会自动从中提取。

相关的地址(relatedAddress)和端口完全不被 ICE 本身使用;它们仅用于分析和诊断目的,并且其包含可能会被安全系统阻止,因此不要依赖它们拥有非 null 的值。

一个无符号 16 位值,包含候选者的相关端口号(如果存在)。对于对等反射(peer reflexive)和服务器反射(server reflexive)候选者,相关地址和端口描述了该候选者的基地址。对于中继(relay)候选者,相关地址和端口提供了 TURN 服务器选择的映射地址。

对于主机(host)候选者,relatedPortnull,这意味着该字段未包含在候选者的 a-line 中。

用法说明

相关地址和端口本身并不被 ICE 使用,仅出于诊断和质量服务(Quality-of-Service)的目的而存在。出于安全原因,它们实际上可能会被省略,但如果存在,可以在调试过程中作为有用的工具。请参阅 示例,其中展示了其中一部分。

这是由 STUN 服务器发现的 ICE 候选者的 SDP 属性行(a-line)描述:

a=candidate:4234997325 1 udp 2043278322 192.0.2.172 6502 typ srflx raddr 198.51.100.45 rport 32768 generation 0

远程端口 relatedPort 是 a-line 上 "rport" 标签后面的数字,或者为 32768。

示例

在此示例中,会检查候选者的 type,然后根据候选者类型显示调试输出,包括候选者的类型、地址(ipport)以及相关地址(relatedAddressrelatedPort)。

js
const ip = candidate.address;
const port = candidate.port;
const relIP = candidate.relatedAddress;
const relPort = candidate.relatedPort;

if (relIP && relPort) {
  console.log(
    `Candidate type '${type}' — contact address: ${ip} ${port}, related address: ${relIP} ${relPort}`,
  );
} else {
  console.log(`Host candidate address is ${ip} ${port}`);
}

规范

规范
WebRTC:浏览器中的实时通信
# dom-rtcicecandidate-relatedport

浏览器兼容性

另见