Noa*_*ach 5 javascript firefox events webrtc
我在 Chrome 中有一个可用的 WebRTC 连接。它使用 1 个数据通道作为聊天应用程序的一部分。
我还想支持 Firefox,因此我需要更改一些不支持的事件:对于RTCPeerConnection以及DataChannel。
对数据通道的更改按预期进行:
//chrome implenetation
dc.onopen = this.conncectionStats.bind(this);
dc.onmessage = onMessage;
// chrome and firefox
dc.addEventListener('open', (event) => {
this.conncectionStats.bind(this)
});
dc.addEventListener('message', (event) => {
onMessage(event)
});
Run Code Online (Sandbox Code Playgroud)
但是,更改 PeerConnection 时会出现问题:
// chrome implenetation
pc.onconnectionstatechange = this.onConnectionStateChange.bind(this);
// chrome and firefox
pc.addEventListener('onconnectionstatechange', (event) => {
console.log("onconnectionstatechange fired")
this.onConnectionStateChange.bind(this);
})
Run Code Online (Sandbox Code Playgroud)
该事件从未发生。有什么想法为什么会出现这种情况吗?
该事件应该是正确的,但另一方面,MDN Web Docs上缺少文档。
您应该使用 WebRTC 适配器,以便为您填充不支持的事件: https: //github.com/webrtc/adapter
我在我的网页上使用它,onconnectionstatechange 在 Firefox 中运行良好:
...
pc.onconnectionstatechange = onConnStateChange;
...
function onConnStateChange(event) {
if (pc.connectionState === "failed") {
Terminate();
alert("Connection failed; playback stopped");
}
}
Run Code Online (Sandbox Code Playgroud)