当前位置: 首页 > Web前端 > JavaScript

WebRTC连接状态的集成

时间:2023-03-26 22:25:40 JavaScript

主要是RTCPeerConnection的状态connectionState和iceConnectionState。这两个状态用于对应的onconnectionstatechange和oniceconnectionstatechange事件。mdn文档读头条如下RTCPeerConnection.oniceconnectionstatechangeiceConnectionStateTheICEproxystateanditsconnectiontotheICEserver(STUN,TURN)"new":EstablishinganICEconnectionTheICEproxyiscollectingaddressesorwaitingforremotecandidatestobe可用的。"checking":CollectCandidatesICE代理已经接收到至少一个远程候选,正在检查此时是否有连接可用。候选人可在此期间继续征集。"connected":AvailableCandidatesMatched已为所有连接的组件找到可用的本地和远程候选对,并且已建立连接。此时,仍会测试远程候选人以找到更好的连接。候选人可在此期间继续征集。“完成”:匹配完成,已建立连接ICE代理已找到可用连接,不再测试远程候选人。"failed":NosuitablecandidatesICEcandidate测试了所有remotecandidates,没有找到匹配的candidates。也有可能在一些候选人中发现了一些可用的连接。“disconnected”:网络波动或流断开RTPeerConnection至少有一个组件连接失败。这可能是一种可以自行恢复的临时状态。这句话是一个很好的坑“关闭”:disconnectICE代理被关闭,不再响应任何请求。RTCPeerConnection.onconnectionstatechangeconnectionStatertcice(RTCIceTransport或RTCDtlsTransport(底层传输)类型)的所有传输组件的聚合状态new新创建的连接的至少一个ICE传输组件(RTICETransport或RTCDTLTransport对象)处于新状态,并且不在以下状态:checking,connecting,failed,disconnected,orallconnectionsareintheclosedstate.connecting连接中的一个或多个ICE传输组件当前正在建立连接;即iceConnectionState是checking或者connected,不是closed状态connected至少一个ICE传输组件connected或者completed状态所有的ICE连接要么是inuse(connected或者completed)要么是closed;disconnecteddisconnected至少有一个ICE传输组件处于disconnected状态,其他不处于failed、connecting或checking状态failedconnectionfailureICE传输组件处于failed状态.closed关闭RTCPeerConnection看描述就知道这些两个是派对状态,所以要注意iceConnectionState。当网络波动时,断开和连接都会多次触发oniceconnectionstatechange事件。connectionState的connecting和connected也会触发多次。