RTCPeerConnection.onconnectionstatechange property specifies an EventHandler which is called to handle the connectionstatechange event when it occurs on an instance of RTCPeerConnection . This happens whenever the aggregate state of the connection changes. The aggregate state is a combination of the states of all of the individual network transports being used by the connection.

句法

RTCPeerConnection.onconnectionstatechange = eventHandler;
					

A function which is called by the browser when the connectionstatechange event occurs on the RTCPeerConnection . The function receives as input a single parameter, which is an object of type 事件 . The event object contains no special information of note; you can look at the value of the peer connection's connectionState property to determine what the new state is.

范例

pc.onconnectionstatechange = function(event) {
  switch(pc.connectionState) {
    case "connected":
      // The connection has become fully connected
      break;
    case "disconnected":
    case "failed":
      // One or more transports has terminated unexpectedly or in an error
      break;
    case "closed":
      // The connection has been closed
      break;
  }
}
					

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.onconnectionstatechange' 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
onconnectionstatechange Chrome 72 Edge 79 Firefox No IE No Opera No Safari 11 WebView Android 72 Chrome Android 72 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android 11.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: