这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

RTCPeerConnection.oniceconnectionstatechange property is an event handler which specifies a function to be called when the iceconnectionstatechange event is fired on an RTCPeerConnection instance. This happens when the state of the connection's ICE agent, as represented by the iceConnectionState property, changes.

句法

RTCPeerConnection.oniceconnectionstatechange = eventHandler;
					

This event handler can be set to function which is passed a single input parameter: an 事件 object describing the iceconnectionstatechange event which occurred. Your code can look at the value of RTCPeerConnection.iceConnectionState to determine what the new state is.

范例

The example below watches the state of the ICE agent for a failure or unexpected closure and takes appropriate action, such as presenting an error message or attempting to restart the ICE agent.

pc.oniceconnectionstatechange = function(event) {
  if (pc.iceConnectionState === "failed" ||
      pc.iceConnectionState === "disconnected" ||
      pc.iceConnectionState === "closed") {
    // Handle the failure
  }
};
					

Of course, "disconnected" and "closed" don't necessarily indicate errors; these can be the result of normal ICE negotiation, so be sure to handle these properly (if at all).

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.oniceconnectionstatechange' in that specification.
候选推荐 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
oniceconnectionstatechange Chrome 28 Edge 15 Firefox 22 IE No Opera 43
43
Promise-based version.
不支持 37 — 43
Safari 11 WebView Android Yes Chrome Android 28 Firefox Android 44 Opera Android 43
43
Promise-based version.
不支持 37 — 43
Safari iOS Yes Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: