feat: add KeepAlive packet
This commit is contained in:
+17
-1
@@ -6,7 +6,8 @@ import {
|
|||||||
PublishRequest,
|
PublishRequest,
|
||||||
PublishResponse,
|
PublishResponse,
|
||||||
DiscoverRequest,
|
DiscoverRequest,
|
||||||
DiscoverResponse
|
DiscoverResponse,
|
||||||
|
KeepAlive
|
||||||
} from './Protocol.js'
|
} from './Protocol.js'
|
||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
|
|
||||||
@@ -17,6 +18,8 @@ export class Client extends EventEmitter {
|
|||||||
#ws
|
#ws
|
||||||
#id
|
#id
|
||||||
#address
|
#address
|
||||||
|
#keepAliveInterval
|
||||||
|
#lastPacketDate = 0
|
||||||
|
|
||||||
constructor(gateway) {
|
constructor(gateway) {
|
||||||
super()
|
super()
|
||||||
@@ -33,6 +36,8 @@ export class Client extends EventEmitter {
|
|||||||
const buf = Container.encode(container).finish()
|
const buf = Container.encode(container).finish()
|
||||||
console.log('sending:', buf)
|
console.log('sending:', buf)
|
||||||
this.#ws.send(buf)
|
this.#ws.send(buf)
|
||||||
|
|
||||||
|
this.#lastPacketDate = Date.now()
|
||||||
}
|
}
|
||||||
|
|
||||||
#protoRequest(message) {
|
#protoRequest(message) {
|
||||||
@@ -67,6 +72,15 @@ export class Client extends EventEmitter {
|
|||||||
const auth = AuthenticationResponse.toObject(response.AuthenticationResponse, { longs: String })
|
const auth = AuthenticationResponse.toObject(response.AuthenticationResponse, { longs: String })
|
||||||
this.#address = BigInt(auth.address)
|
this.#address = BigInt(auth.address)
|
||||||
|
|
||||||
|
// send packets periodically to keep the connection alive
|
||||||
|
this.#keepAliveInterval = setInterval(async () => {
|
||||||
|
if ((Date.now() - this.#lastPacketDate) > 30000) {
|
||||||
|
await this.#protoRequest(KeepAlive.create({
|
||||||
|
data: Date.now()
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}, 15000)
|
||||||
|
|
||||||
this.emit('ready')
|
this.emit('ready')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +95,8 @@ export class Client extends EventEmitter {
|
|||||||
|
|
||||||
ws.onclose = () => {
|
ws.onclose = () => {
|
||||||
this.emit('close')
|
this.emit('close')
|
||||||
|
clearInterval(this.#keepAliveInterval)
|
||||||
|
this.#keepAliveInterval = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-3
@@ -18,7 +18,8 @@ export const [
|
|||||||
PublishRequest,
|
PublishRequest,
|
||||||
PublishResponse,
|
PublishResponse,
|
||||||
DiscoverRequest,
|
DiscoverRequest,
|
||||||
DiscoverResponse
|
DiscoverResponse,
|
||||||
|
KeepAlive
|
||||||
] = import_proto(encoders, [
|
] = import_proto(encoders, [
|
||||||
'Container',
|
'Container',
|
||||||
'AuthenticationRequest',
|
'AuthenticationRequest',
|
||||||
@@ -27,7 +28,8 @@ export const [
|
|||||||
'PublishRequest',
|
'PublishRequest',
|
||||||
'PublishResponse',
|
'PublishResponse',
|
||||||
'DiscoverRequest',
|
'DiscoverRequest',
|
||||||
'DiscoverResponse'
|
'DiscoverResponse',
|
||||||
|
'KeepAlive'
|
||||||
])
|
])
|
||||||
|
|
||||||
export const messages = [
|
export const messages = [
|
||||||
@@ -38,5 +40,6 @@ export const messages = [
|
|||||||
PublishRequest,
|
PublishRequest,
|
||||||
PublishResponse,
|
PublishResponse,
|
||||||
DiscoverRequest,
|
DiscoverRequest,
|
||||||
DiscoverResponse
|
DiscoverResponse,
|
||||||
|
KeepAlive
|
||||||
]
|
]
|
||||||
|
|||||||
+1
-1
Submodule src/protocol updated: d0747faf68...1fe4783d94
Reference in New Issue
Block a user