Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make time duration since last server data available #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/amqp-base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export abstract class AMQPBaseClient {
channelMax = 0
frameMax: number
heartbeat: number
lastDataReceived: number | undefined = undefined
onerror: (error: AMQPError) => void
logger: Logger | null | undefined = console
/** Used for string -> arraybuffer when publishing */
Expand Down Expand Up @@ -85,6 +86,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 @@ -606,5 +608,18 @@ export abstract class AMQPBaseClient {
}
i += 1 // frame end
}
this.lastDataReceived = performance.now()
}

/**
* Get the time since since connection or last data received
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Get the time since since connection or last data received
* Get the time 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 @@ -58,6 +58,7 @@ export class AMQPClient extends AMQPBaseClient {
socket.setTimeout((this.heartbeat || 60) * 1000)
// enable TCP keepalive if AMQP heartbeats are disabled
if (this.heartbeat === 0) socket.setKeepAlive(true, 60)
this.lastDataReceived = performance.now()
return new Promise((resolve, reject) => {
socket.on('timeout', () => reject(new AMQPError("timeout", this)))
socket.on('error', (err) => reject(new AMQPError(err.message, this)))
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.addEventListener('close', reject)
Expand Down
Loading