RTCPeerConnection: connectionState 属性

Baseline 2023
新推出

自 ⁨2023 年 5 月⁩起,此功能可在最新的设备和浏览器版本上使用。此功能可能无法在旧版设备或浏览器上使用。

RTCPeerConnection 接口的只读属性 connectionState 通过返回以下字符串值之一来指示对等连接的当前状态:newconnectingconnecteddisconnectedfailedclosed

此状态本质上代表了连接使用的所有 ICE 传输(类型为 RTCIceTransportRTCDtlsTransport)的聚合状态。

当此属性的值发生更改时,一个 connectionstatechange 事件将被发送到 RTCPeerConnection 实例。

一个表示连接当前状态的字符串。它可以取以下值之一:

new

连接的 ICE 传输(RTCIceTransportRTCDtlsTransport 对象)中至少有一个处于 new 状态,并且它们中没有处于以下状态之一:connectingcheckingfaileddisconnected,或者连接的所有传输都处于 closed 状态。

connecting

ICE 传输中有一个或多个目前正处于建立连接的过程中;也就是说,它们的 iceConnectionState 要么是 checking,要么是 connected,并且没有传输处于 failed 状态。

connected

连接使用的每个 ICE 传输都处于使用中(状态为 connectedcompleted)或已关闭(状态为 closed);此外,至少有一个传输处于 connectedcompleted 状态。

disconnected

连接的 ICE 传输中至少有一个处于 disconnected 状态,并且其他传输没有处于以下状态:failedconnectingchecking

failed

连接中的一个或多个 ICE 传输处于 failed 状态。

closed

RTCPeerConnection 已关闭。

示例

js
const peerConnection = new RTCPeerConnection(configuration);

// …

const connectionState = peerConnection.connectionState;

规范

规范
WebRTC:浏览器中的实时通信
# dom-peerconnection-connection-state

浏览器兼容性

另见