Skip to content

Commit

Permalink
add console logs for relay events
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose committed Jul 9, 2024
1 parent 9cd179c commit 09ac8b4
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions test/manual/measure-reconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
*/

function customLogger (data) {
console.log(` ... ${data.id} ${Object.keys(data.caller.props || []).join(',')} ${data.caller.filename}:${data.caller.line}:${data.caller.column}`)
console.log(` ... ${data.object.className}.${data.id} ${Object.keys(data.caller.props || []).join(',')} ${data.caller.filename}:${data.caller.line}:${data.caller.column}`)
}
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)
}
require('hypertrace').setTraceFunction(customLogger)

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 @@ -22,6 +29,7 @@ const topic = Buffer.alloc(32).fill('measure-reconnect')
const seed = Buffer.alloc(32).fill('measure-reconnect' + require('os').hostname())

const swarm = new Hyperswarm({ seed, relayThrough })
console.log(`PUBLIC_KEY ${swarm.keyPair.publicKey.toString('hex').slice(0, 8)}...`)

swarm.dht.on('network-change', () => {
console.log('NETWORK CHANGE')
Expand All @@ -31,9 +39,14 @@ swarm.dht.on('network-change', () => {
let connected = false

swarm.on('connection', async (conn) => {
console.log(conn.rawStream.remoteHost)
conn.on('error', console.log.bind(console))
conn.on('close', console.log.bind(console))
console.log(conn.rawStream.remoteHost + ':' + conn.rawStream.remotePort)

conn.relay.on('relay', () => console.log('RELAY: RELAY'))
conn.relay.on('unrelay', () => console.log('RELAY: UNRELAY'))
conn.relay.on('abort', () => console.log('RELAY: ABORTED'))

conn.on('error', (...args) => console.log('error:', ...args))
conn.on('close', (...args) => console.log('close:', ...args))
conn.on('data', (data) => console.log(data.toString('utf8')))
conn.setKeepAlive(5000)
conn.write('hello')
Expand All @@ -48,6 +61,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 09ac8b4

Please sign in to comment.