我有一个简单的WebRTC应用程序,仅传输文本数据。除了处理睡眠模式外,它都很好用。
例如,我在计算机的两个不同选项卡上与两个用户进行本地测试,然后将计算机(macOS)置于睡眠模式。不同的情况:
为什么onclose事件在另一侧触发时却没有在Firefox侧触发?
是否有替代解决方案来查找连接是否断开?
如果有帮助,我的代码的一部分:
const configuration = {
iceServers: [
{urls: 'stun:stun.l.google.com:19302'},
{urls: 'turn:numb.viagenie.ca', username: '...', credential: '...'},
]
}
// Keep track of all RTCPeerConnection.
// Key is userId aka peerId, value is object with 'conn' and 'dataChannel' keys.
let peers = {}
const onNewPeer = (peerId, username) => {
if (peerId in peers) {
log('Already connected to peer ' + peerId)
return
}
const peerconn = new RTCPeerConnection(configuration)
// 'onicecandidate' notifies us whenever an ICE agent needs to deliver a
// message to the other peer through the signaling server
peerconn.onicecandidate = event => {
if (event.candidate)
sendSignalingMessage({ candidate: event.candidate, toUserId: peerId })
}
// OFFERER SPECIFIC PART
peerconn.onnegotiationneeded = () => {
peerconn.createOffer().then(desc => setLocalDescription(desc, peerId), error => console.error(error))
}
const dataChannel = peerconn.createDataChannel('chat') // RTCDataChannel
setupDataChannel(dataChannel, peerId, username)
sendSignalingMessage({ type: 'ADD_PEER_ANSWER', toUserId: peerId })
// END OFFERER SPECIFIC
peers[peerId] = { conn: peerconn, username: username, dataChannel: dataChannel }
}
const onNewPeerAnswer = (peerId, username) => {
if (peerId in peers) {
log('Already connected to peer ', peerId)
return
}
const peerconn = new RTCPeerConnection(configuration)
// 'onicecandidate' notifies us whenever an ICE agent needs to deliver a
// message to the other peer through the signaling server
peerconn.onicecandidate = event => {
if (event.candidate)
sendSignalingMessage({ candidate: event.candidate, toUserId: peerId })
}
// ANSWERER SPECIFIC PART
// If user is not the offerer, let's wait for a data channel
peerconn.ondatachannel = event => {
const dataChannel = event.channel // RTCDataChannel
setupDataChannel(dataChannel, peerId, username)
peers[peerId]['dataChannel'] = dataChannel
}
// END ANSWERER SPECIFIC
peers[peerId] = { conn: peerconn, username: username }
}
const setupDataChannel = (dataChannel, peerId, username) => {
dataChannel.onopen = () => {
console.log(`%cYou are now connected to ${peerId} aka ${username}`, 'color: green;')
}
// PROBLEM: this is triggered on Chrome when going to sleep,
// but on firefox side this is not triggered and firefox still
// thinks this dataChannel is opened after waking up.
dataChannel.onclose = () => {
delete peers[peerId]
console.log(`%cDisconnected from ${peerId} aka ${username}`, 'color: red;')
}
dataChannel.onerror = () => {
delete peers[peerId]
console.log(`%cDisconnected from ${peerId} aka ${username} (onerror)`, 'color: red;')
}
dataChannel.onmessage = event => {
log('Received through WebRTC: ', JSON.parse(event.data))
}
}