From 16e85030e78ed9edb2ebecf81bac3ad33d622111 Mon Sep 17 00:00:00 2001 From: Chad Nehemiah Date: Tue, 25 Apr 2023 13:52:11 -0500 Subject: [PATCH] fix: added peer connection state listener to emit closed events (#134) * 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) --- src/maconn.ts | 4 +++- src/peer_transport/transport.ts | 2 +- src/peer_transport/util.ts | 2 +- src/transport.ts | 25 ++++++++++++++++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/maconn.ts b/src/maconn.ts index 0814874870..8720b8e7bd 100644 --- a/src/maconn.ts +++ b/src/maconn.ts @@ -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() } } diff --git a/src/peer_transport/transport.ts b/src/peer_transport/transport.ts index b625e4c201..98a132b872 100644 --- a/src/peer_transport/transport.ts +++ b/src/peer_transport/transport.ts @@ -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 }), { diff --git a/src/peer_transport/util.ts b/src/peer_transport/util.ts index 862649a510..627d12351f 100644 --- a/src/peer_transport/util.ts +++ b/src/peer_transport/util.ts @@ -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 diff --git a/src/transport.ts b/src/transport.ts index 6c6a452c1a..38f6382482 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -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') @@ -97,6 +98,9 @@ export class WebRTCDirectTransport implements Transport { * Connect to a peer using a multiaddr */ async _connect (ma: Multiaddr, options: WebRTCDialOptions): Promise { + 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") @@ -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() } })