add `libp2p` connection listeners
This commit is contained in:
parent
3d5c490185
commit
5cef43991f
|
@ -3,7 +3,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { hexToBytes } from 'ethereum-cryptography/utils'
|
import { hexToBytes } from 'ethereum-cryptography/utils'
|
||||||
import { Protocols } from 'js-waku'
|
import { Protocols, waku_filter, waku_light_push, waku_store } from 'js-waku'
|
||||||
import { createLightNode } from 'js-waku/lib/create_waku'
|
import { createLightNode } from 'js-waku/lib/create_waku'
|
||||||
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
|
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
|
||||||
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
|
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
|
||||||
|
@ -21,6 +21,12 @@ import type { Storage } from './storage'
|
||||||
import type { WakuLight } from 'js-waku/lib/interfaces'
|
import type { WakuLight } from 'js-waku/lib/interfaces'
|
||||||
import type { MessageV1 as WakuMessage } from 'js-waku/lib/waku_message/version_1'
|
import type { MessageV1 as WakuMessage } from 'js-waku/lib/waku_message/version_1'
|
||||||
|
|
||||||
|
const PROTOCOLS = [Protocols.Store, Protocols.Filter, Protocols.LightPush]
|
||||||
|
const CODECS = [
|
||||||
|
waku_store.StoreCodec,
|
||||||
|
waku_filter.FilterCodec,
|
||||||
|
waku_light_push.LightPushCodec,
|
||||||
|
]
|
||||||
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
|
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
|
||||||
|
|
||||||
export interface ClientOptions {
|
export interface ClientOptions {
|
||||||
|
@ -41,17 +47,18 @@ class Client {
|
||||||
public readonly wakuMessages: Set<string>
|
public readonly wakuMessages: Set<string>
|
||||||
/**
|
/**
|
||||||
* Tracks open connections which had their streams silently destroyed
|
* Tracks open connections which had their streams silently destroyed
|
||||||
* and closes them so new connections can be automatically created.
|
* and closes them so new connections can be automatically created
|
||||||
|
* by libp2p's dial mechanism.
|
||||||
*
|
*
|
||||||
* For example, in case of a websocket in browser which did not
|
* For example, in case of a websocket in browser which did not
|
||||||
* have its close event emitted.
|
* have its close event emitted.
|
||||||
*
|
*
|
||||||
* Note: Detection of the stream removal depends on active (by user)
|
* Note: Detection of the stream removal depends pinging the peer.
|
||||||
* or pasive (with `Waku.relayKeepAlive`) message sending.
|
|
||||||
*
|
*
|
||||||
* Note: This is only a workaround (@see https://github.com/libp2p/js-libp2p/issues/939).
|
* Note: This is only a workaround (@see https://github.com/libp2p/js-libp2p/issues/939).
|
||||||
*/
|
*/
|
||||||
#wakuDisconnectionTimer: ReturnType<typeof setInterval>
|
#wakuDisconnectionTimer: ReturnType<typeof setInterval>
|
||||||
|
connected: boolean
|
||||||
|
|
||||||
public activityCenter: ActivityCenter
|
public activityCenter: ActivityCenter
|
||||||
public community: Community
|
public community: Community
|
||||||
|
@ -78,6 +85,33 @@ class Client {
|
||||||
|
|
||||||
await Promise.allSettled(connectionsToClose)
|
await Promise.allSettled(connectionsToClose)
|
||||||
}, 10 * 1000)
|
}, 10 * 1000)
|
||||||
|
/**
|
||||||
|
* Note: Assumes 1 remote node.
|
||||||
|
*/
|
||||||
|
this.waku.libp2p.connectionManager.addEventListener(
|
||||||
|
'peer:connect',
|
||||||
|
async event => {
|
||||||
|
const connection = event.detail
|
||||||
|
const protocols = await this.waku.libp2p.peerStore.protoBook.get(
|
||||||
|
connection.remotePeer
|
||||||
|
)
|
||||||
|
const isMissingProtocol = CODECS.some(
|
||||||
|
codec => !protocols.includes(codec)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!isMissingProtocol) {
|
||||||
|
this.connected = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
/**
|
||||||
|
* >This event will **only** be triggered when the last connection is closed.
|
||||||
|
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
|
||||||
|
*/
|
||||||
|
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
|
||||||
|
this.connected = false
|
||||||
|
})
|
||||||
|
this.connected = true
|
||||||
|
|
||||||
// Storage
|
// Storage
|
||||||
this.storage = options.storage ?? new LocalStorage()
|
this.storage = options.storage ?? new LocalStorage()
|
||||||
|
@ -129,11 +163,7 @@ class Client {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
await waku.start()
|
await waku.start()
|
||||||
await waitForRemotePeer(
|
await waitForRemotePeer(waku, PROTOCOLS, 10 * 1000)
|
||||||
waku,
|
|
||||||
[Protocols.Store, Protocols.Filter, Protocols.LightPush],
|
|
||||||
10 * 1000
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client
|
// Client
|
||||||
client = new Client(waku, options)
|
client = new Client(waku, options)
|
||||||
|
|
Loading…
Reference in New Issue