Skip to content

Commit

Permalink
fix: maintain correct reference to peer to destroy upon disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Dec 4, 2022
1 parent 9c86b65 commit 50864cc
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => {
)
)
peer.on(events.connect, () => onConnect(peer, val.peer_id))
peer.on(events.close, () => onDisconnect(val.peer_id, val.offer_id))
peer.on(events.close, () => onDisconnect(peer, val.peer_id, val.offer_id))
peer.signal(
key ? {...val.offer, sdp: await decrypt(key, val.offer.sdp)} : val.offer
)
Expand All @@ -152,7 +152,7 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => {
peer.on(events.connect, () =>
onConnect(peer, val.peer_id, val.offer_id)
)
peer.on(events.close, () => onDisconnect(val.peer_id, val.offer_id))
peer.on(events.close, () => onDisconnect(peer, val.peer_id, val.offer_id))
peer.signal(
key
? {...val.answer, sdp: await decrypt(key, val.answer.sdp)}
Expand Down Expand Up @@ -242,17 +242,11 @@ export const joinRoom = initGuard(occupiedRooms, (config, ns) => {
}
}

const onDisconnect = (peerId, offerId) => {
const onDisconnect = (peer, peerId, offerId) => {
delete connectedPeers[peerId]
const { peer } = offerPool[offerId]

if (peer) {
peer.destroy()
delete offerPool[offerId]
offerPool = {...offerPool, ...makeOffers(1)}
} else {
console.error(`peer ${peerId} for offer ${offerId} not found`)
}
delete offerPool[offerId]
peer.destroy()
offerPool = {...offerPool, ...makeOffers(1)}
}

let announceSecs = defaultAnnounceSecs
Expand Down

0 comments on commit 50864cc

Please sign in to comment.