Skip to content

Commit

Permalink
Make time duration since last server data available
Browse files Browse the repository at this point in the history
  • Loading branch information
ngbrown committed Jul 25, 2023
1 parent c85a6ab commit 1eaf4be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/amqp-base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class AMQPBaseClient {
channelMax = 0
frameMax: number
heartbeat: number
lastDataReceived: number | undefined = undefined
onerror: (error: AMQPError) => void
/** Used for string -> arraybuffer when publishing */
readonly textEncoder = new TextEncoder()
Expand Down Expand Up @@ -94,6 +95,7 @@ export abstract class AMQPBaseClient {
close(reason = "", code = 200): Promise<void> {
if (this.closed) return this.rejectClosed()
this.closed = true
this.lastDataReceived = undefined
let j = 0
const frame = new AMQPView(new ArrayBuffer(512))
frame.setUint8(j, 1); j += 1 // type: method
Expand Down Expand Up @@ -589,5 +591,18 @@ export abstract class AMQPBaseClient {
}
i += 1 // frame end
}
this.lastDataReceived = performance.now()
}

/**
* Get the time since since connection or last data received
* @returns milliseconds; Infinity if disconnected
*/
durationSinceLastData(): number {
if (typeof this.lastDataReceived === 'number') {
return performance.now() - this.lastDataReceived;
} else {
return Infinity
}
}
}
1 change: 1 addition & 0 deletions src/amqp-socket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class AMQPClient extends AMQPBaseClient {
writable: true,
enumerable: false // hide it from console.log etc.
})
this.lastDataReceived = performance.now()
return new Promise((resolve, reject) => {
socket.on('error', (err) => reject(new AMQPError(err.message, this)))
this.connectPromise = [resolve, reject]
Expand Down
1 change: 1 addition & 0 deletions src/amqp-websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class AMQPWebSocketClient extends AMQPBaseClient {
this.socket = socket
socket.binaryType = "arraybuffer"
socket.onmessage = this.handleMessage.bind(this)
this.lastDataReceived = performance.now()
return new Promise((resolve, reject) => {
this.connectPromise = [resolve, reject]
socket.onclose = reject
Expand Down

0 comments on commit 1eaf4be

Please sign in to comment.