feat: add KeepAlive packet

This commit is contained in:
2023-07-15 14:15:48 +02:00
parent 4ed86f2e88
commit bafa913506
3 changed files with 26 additions and 7 deletions
+18 -3
View File
@@ -7,7 +7,8 @@ import {
PublishResponse,
DiscoverRequest,
DiscoverResponse,
DiscoverPeer
DiscoverPeer,
KeepAlive
} from './Protocol.js'
import { EventEmitter } from 'node:events'
@@ -30,6 +31,7 @@ export class Peer extends EventEmitter {
this.#msgMap.set('Message', this.#onmessage)
this.#msgMap.set('PublishRequest', this.#onpublishrequest)
this.#msgMap.set('DiscoverRequest', this.#ondiscoverrequest)
this.#msgMap.set('KeepAlive', this.#onkeepalive)
this.#setupListeners()
}
@@ -98,10 +100,9 @@ export class Peer extends EventEmitter {
const hash = createHash('sha256').update(this.#salt + seed).digest('hex')
const address = BigInt('0x' + hash.substring(0, 16))
console.log('assigned address', address.toString(16).match(/.{1,4}/g).join(':'))
this.#address = address
console.log('assigned address', this.formattedAddress)
console.log(address.toString(10))
this.#send({
id: message.id,
message: AuthenticationResponse.create({
@@ -183,7 +184,21 @@ export class Peer extends EventEmitter {
})
}
#onkeepalive(message) {
const keepAlive = KeepAlive.toObject(message.KeepAlive)
console.log(`keepalive from ${this.#address}: ${keepAlive.data}`)
this.#send({
id: message.id,
message: keepAlive
})
}
get address() {
return this.#address
}
get formattedAddress() {
return this.#address.toString(16).match(/.{1,4}/g).join(':')
}
}
+7 -3
View File
@@ -13,11 +13,11 @@ export const [
export const [
AuthenticationRequest,
AuthenticationResponse
] = await import_proto('2.0/Authentication.proto', ['AuthenticationRequest', 'AuthenticationResponse'])
] = await import_proto('2.0/Authentication.proto', ['AuthenticationRequest', 'AuthenticationResponse'])
export const [
Message
] = await import_proto('2.0/Message.proto', ['Message'])
] = await import_proto('2.0/Message.proto', ['Message'])
export const [
PublishRequest,
@@ -25,7 +25,11 @@ export const [
DiscoverRequest,
DiscoverResponse,
DiscoverPeer
] = await import_proto('2.0/PublishDiscover.proto', ['PublishRequest', 'PublishResponse', 'DiscoverRequest', 'DiscoverResponse', 'DiscoverPeer'])
] = await import_proto('2.0/PublishDiscover.proto', ['PublishRequest', 'PublishResponse', 'DiscoverRequest', 'DiscoverResponse', 'DiscoverPeer'])
export const [
KeepAlive
] = await import_proto('2.0/KeepAlive.proto', ['KeepAlive'])
export const messages = [
Container,