Skip to content

Commit

Permalink
add 'relaying' event to swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose committed Jul 2, 2024
1 parent 9cd179c commit 621375b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ module.exports = class Hyperswarm extends EventEmitter {
this._clientConnections++
let opened = false

conn.on('relaying', ({ relayThrough }) => {
this.emit('relaying', {
publicKey: peerInfo.publicKey,
relayThrough
})
})

conn.on('open', () => {
opened = true
this.stats.connects.client.opened++
Expand Down
19 changes: 15 additions & 4 deletions test/manual/measure-reconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
function customLogger (data) {
console.log(` ... ${data.id} ${Object.keys(data.caller.props || []).join(',')} ${data.caller.filename}:${data.caller.line}:${data.caller.column}`)
}
require('hypertrace').setTraceFunction(customLogger)
try {
require('hypertrace').setTraceFunction(customLogger)
} catch {
console.log('Please run:')
console.log('npm install --no-save hypertrace hypercore-id-encoding @holepunchto/keet-default-config')
process.exit(1)
}

const { DEV_BLIND_RELAY_KEYS } = require('@holepunchto/keet-default-config')
const HypercoreId = require('hypercore-id-encoding')
const DEV_RELAY_KEYS = DEV_BLIND_RELAY_KEYS.map(HypercoreId.decode)
console.log('DEV_RELAY_KEYS', DEV_RELAY_KEYS.map(b => b.toString('hex').slice(0, 8) + '...'))
const relayThrough = (force) => force ? DEV_RELAY_KEYS : null

const Hyperswarm = require('../..')
Expand All @@ -30,6 +37,10 @@ swarm.dht.on('network-change', () => {

let connected = false

swarm.on('relaying', ({ publicKey, relayThrough }) => {
console.log(`connecting to ${publicKey.toString('hex').slice(0, 8)}... via ${relayThrough.toString('hex').slice(0, 8)}...`)
})

swarm.on('connection', async (conn) => {
console.log(conn.rawStream.remoteHost)
conn.on('error', console.log.bind(console))
Expand All @@ -48,6 +59,6 @@ swarm.on('connection', async (conn) => {
console.time('INITIAL CONNECTION TIME')
swarm.join(topic)

// process.on('SIGINT', () => {
// swarm.leave(topic).then(() => process.exit())
// })
process.on('SIGINT', () => {
swarm.leave(topic).then(() => process.exit())
})

0 comments on commit 621375b

Please sign in to comment.