RTCPeerConnection.connectionState

connectionState 只读属性表示了当前所有被使用的 ICE 连接的状态,其返回值为以下字符串之一:newconnectingconnecteddisconnectedfailedclosed

该状态本质上表示所有使用的 ICE 连接(类型为 RTCIceTransport (en-US)RTCDtlsTransport (en-US))的聚合状态。

当属性值变化时,一个 connectionstatechange (en-US) 事件将被发送给 RTCPeerConnection 对象实例。

语法

js
var connectionState = RTCPeerConnection.connectionState;

返回值

一个用于表示当前连接状态的字符串,为以下列出的值之一:

new

表示至少有一个 ICE 连接(RTCIceTransport (en-US)RTCDtlsTransport (en-US) 对象)处于 new 状态,并且没有连接处于以下状态: connectingcheckingfaileddisconnected,或者这些连接都处于 closed 状态。

connecting

表示至少有一个 ICE 连接处于正在建立连接的状态;也就是说,它们的 iceConnectionState 值为 checkingconnected,并且没有连接处于 failed 状态。

connected

表示每一个 ICE 连接要么正在使用(connectedcompleted 状态),要么已被关闭(closed 状态);并且,至少有一个连接处于 connectedcompleted 状态。

disconnected

表示至少有一个 ICE 连接处于 disconnected 状态,并且没有连接处于 failedconnectingchecking 状态。

failed

表示至少有一个 ICE 连接处于 failed 的状态。

closed

表示 RTCPeerConnection 已关闭。

示例

js
var pc = new RTCPeerConnection(configuration);

/* ... */

var connectionState = pc.connectionState;

规范

Specification
WebRTC: Real-Time Communication in Browsers
# dom-peerconnection-connection-state

浏览器兼容性

BCD tables only load in the browser

参见