Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
fix: added peer connection state listener to emit closed events (#134)
Browse files Browse the repository at this point in the history
* fix: added peer connection state listener to emit closed events

* fix: updated listening event name for disconnect/closed events (#138)

* fix: removed listener once connection is closed (#138)

* fix: removed listener once connection is closed (#138)
  • Loading branch information
maschad committed Apr 25, 2023
1 parent d6e5784 commit 16e8503
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/maconn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection {
log.error('error closing connection', err)
}

this.timeline.close = new Date().getTime()
log.trace('closing connection')

this.timeline.close = Date.now()
this.peerConnection.close()
}
}
2 changes: 1 addition & 1 deletion src/peer_transport/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class WebRTCTransport implements Transport, Startable {
const result = await options.upgrader.upgradeOutbound(
new WebRTCMultiaddrConnection({
peerConnection: pc,
timeline: { open: (new Date()).getTime() },
timeline: { open: Date.now() },
remoteAddr: webrtcMultiaddr
}),
{
Expand Down
2 changes: 1 addition & 1 deletion src/peer_transport/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as pb from './pb/index.js'
import { detect } from 'detect-browser'

const browser = detect()
const isFirefox = ((browser != null) && browser.name === 'firefox')
export const isFirefox = ((browser != null) && browser.name === 'firefox')

interface MessageStream {
read: () => Promise<pb.Message>
Expand Down
25 changes: 24 additions & 1 deletion src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as sdp from './sdp.js'
import { WebRTCStream } from './stream.js'
import { genUfrag } from './util.js'
import { protocols } from '@multiformats/multiaddr'
import { isFirefox } from './peer_transport/util.js'

const log = logger('libp2p:webrtc:transport')

Expand Down Expand Up @@ -97,6 +98,9 @@ export class WebRTCDirectTransport implements Transport {
* Connect to a peer using a multiaddr
*/
async _connect (ma: Multiaddr, options: WebRTCDialOptions): Promise<Connection> {
const controller = new AbortController()
const signal = controller.signal

const remotePeerString = ma.getPeerId()
if (remotePeerString === null) {
throw inappropriateMultiaddr("we need to have the remote's PeerId")
Expand Down Expand Up @@ -183,13 +187,32 @@ export class WebRTCDirectTransport implements Transport {
}
}

const eventListeningName = isFirefox ? 'iceconnectionstatechange' : 'connectionstatechange'

peerConnection.addEventListener(eventListeningName, () => {
switch (peerConnection.connectionState) {
case 'failed':
case 'disconnected':
case 'closed':
maConn.close().catch((err) => {
log.error('error closing connection', err)
}).finally(() => {
// Remove the event listener once the connection is closed
controller.abort()
})
break
default:
break
}
}, { signal })

// Creating the connection before completion of the noise
// handshake ensures that the stream opening callback is set up
const maConn = new WebRTCMultiaddrConnection({
peerConnection,
remoteAddr: ma,
timeline: {
open: (new Date()).getTime()
open: Date.now()
}
})

Expand Down

0 comments on commit 16e8503

Please sign in to comment.