From da911d7f6d993061c731fd702e810408aab62dcc Mon Sep 17 00:00:00 2001 From: satoren Date: Mon, 25 Nov 2024 18:04:53 +0900 Subject: [PATCH] feat: Separate heart beat interval and timeout Added comment to clarify that `sendHeartbeat` is not private for allows Heartbeat to be sent at any time --- assets/js/phoenix/socket.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/assets/js/phoenix/socket.js b/assets/js/phoenix/socket.js index 8da13dd92d..27c1796fd8 100644 --- a/assets/js/phoenix/socket.js +++ b/assets/js/phoenix/socket.js @@ -53,6 +53,7 @@ import Timer from "./timer" * * Defaults `DEFAULT_TIMEOUT` * @param {number} [opts.heartbeatIntervalMs] - The millisec interval to send a heartbeat message + * @param {number} [opts.heartbeatTimeoutMs] - The default timeout in milliseconds to trigger heartbeat timeouts * @param {Function} [opts.reconnectAfterMs] - The optional function that returns the * socket reconnect interval, in milliseconds. * @@ -148,6 +149,7 @@ export default class Socket { }) } this.heartbeatIntervalMs = opts.heartbeatIntervalMs || 30000 + this.heartbeatTimeoutMs = opts.heartbeatTimeoutMs || this.heartbeatIntervalMs this.rejoinAfterMs = (tries) => { if(opts.rejoinAfterMs){ return opts.rejoinAfterMs(tries) @@ -600,11 +602,14 @@ export default class Socket { return this.ref.toString() } + /** + * Send heat beat message. If the response timeout, Attempting to re-establish connection + */ sendHeartbeat(){ if(this.pendingHeartbeatRef && !this.isConnected()){ return } this.pendingHeartbeatRef = this.makeRef() this.push({topic: "phoenix", event: "heartbeat", payload: {}, ref: this.pendingHeartbeatRef}) - this.heartbeatTimeoutTimer = setTimeout(() => this.heartbeatTimeout(), this.heartbeatIntervalMs) + this.heartbeatTimeoutTimer = setTimeout(() => this.heartbeatTimeout(), this.heartbeatTimeoutMs) } flushSendBuffer(){