An
iceconnectionstatechange
event is sent to an
RTCPeerConnection
object each time the
ICE
connection state changes during the negotiation process.
The new ICE connection state is available in the object's
iceConnectionState
} property.
| 冒泡 | No |
|---|---|
| 可取消 | No |
| 接口 |
事件
|
| 事件处理程序特性 |
oniceconnectionstatechange
|
One common task performed by the
iceconnectionstatechange
event listener: to trigger ICE restart when the state changes to
failed
。见
ICE restart
in
Lifetime of a WebRTC session
了解进一步信息。
A successful connection attempt will typically involve the state starting at
new
, then transitioning through
checking
,那么
connected
, and finally
completed
. However, under certain circumstances, the
connected
state can be skipped, causing a connection to transition directly from the
checking
state to
completed
. This can happen when only the last checked candidate is successful, and the gathering and end-of-candidates signals both occur before the successful negotiation is completed.
When an ICE restart is processed, the gathering and connectivity checking process is started over from the beginning, which will cause the
iceConnectionState
to transition to
connected
if the ICE restart was triggered while in the
completed
state. If ICE restart is initiated while in the transient
disconnected
state, the state transitions instead to
checking
, essentially indicating that the negotiation is ignoring the fact that the connection had been temporarily lost.
When the negotiation process runs out of candidates to check, the ICE connection transitions to one of two states. If no suitable candidates were found, the state transitions to
failed
. If at least one suitable candidate was successfully identified, the state transitions to
completed
. The ICE layer makes this determination upon receiving the end-of-candidates signal, which is provided by caling
addIceCandidate()
with a candidate whose
candidate
property is an empty string (""), or by setting the
RTCPeerConnection
property
canTrickleIceCandidates
to
false
.
An event handler for this event can be added using the
RTCPeerConnection.oniceconnectionstatechange
property or by using
addEventListener()
在
RTCPeerConnection
.
In this example, a handler for
iceconnectionstatechange
is set up to update a call state indicator by using the value of
iceConnectionState
to create a string which corresponds to the name of a CSS class that we can assign to the status indicator to cause it to reflect the current state of the connection.
pc.addEventListener("iceconnectionstatechange", ev => {
let stateElem = document.querySelector("#call-state");
stateElem.className = `${pc.iceConnectionState}-state`;
}, false);
This can also be written as:
pc.oniceconnectionstatechange = ev => {
let stateElem = document.querySelector("#call-state");
stateElem.className = `${pc.iceConnectionState}-state`;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'iceconnectionstatechange' in that specification. |
候选推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
iceconnectionstatechange
event
|
Chrome 28 | Edge 15 | Firefox 22 | IE No | Opera 43 | Safari 11 | WebView Android Yes | Chrome Android 28 | Firefox Android 44 | Opera Android 43 | Safari iOS ? | Samsung Internet Android 6.0 |
完整支持
不支持
兼容性未知
RTCPeerConnection
RTCPeerConnection.onIceConnectionStateChange
RTCPeerConnection.iceConnectionState
RTCPeerConnection
canTrickleIceCandidates
connectionState
currentLocalDescription
currentRemoteDescription
getDefaultIceServers()
iceConnectionState
iceGatheringState
localDescription
onaddstream
onconnectionstatechange
ondatachannel
onicecandidate
oniceconnectionstatechange
onicegatheringstatechange
onidentityresult
onidpassertionerror
onidpvalidationerror
onnegotiationneeded
onpeeridentity
onremovestream
onsignalingstatechange
ontrack
peerIdentity
pendingLocalDescription
pendingRemoteDescription
remoteDescription
sctp
signalingState
addIceCandidate()
addStream()
addTrack()
close()
createAnswer()
createDataChannel()
createOffer()
generateCertificate()
getConfiguration()
getIdentityAssertion()
getReceivers()
getSenders()
getStats()
getStreamById()
getTransceivers()
removeStream()
removeTrack()
restartIce()
setConfiguration()
setIdentityProvider()
setLocalDescription()
setRemoteDescription()
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannel
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent