Skip to content

Commit e842376

Browse files
committed
fix: fix websocket keepalive timer cleanup
Ref: #77 Signed-off-by: Philip Gerke <me@philipgerke.com>
1 parent 2d6bb60 commit e842376

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://
77
semver.org/spec/v2.0.0.html).
88

9+
## 1.6.1 - 17.05.2025
10+
11+
### Fixed
12+
13+
- [#77](https://github.com/pgerke/freeathome-local-api-client/issues/77):
14+
Fixed an issue that caused multiple websocket keep alive timer to be configured simultaneously
15+
916
## 1.6.0 - 16.05.2025
1017

1118
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "freeathome-local-api-client",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"preview": false,
55
"description": "A client library for the BUSCH-JAEGER free@home local API implemented in TypeScript",
66
"repository": {

src/system-access-point.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ export class SystemAccessPoint extends EventEmitter {
8686
}
8787

8888
this.webSocket = this.createWebSocket(certificateVerification);
89+
// (Re-)create the web socket keepalive timer
90+
this.webSocketKeepaliveSubscription?.unsubscribe();
91+
this.webSocketKeepaliveSubscription = undefined;
8992
this.webSocketKeepaliveSubscription =
9093
this.webSocketKeepaliveTimer$.subscribe(() => {
9194
if (!(this.webSocket && this.webSocket.readyState === WebSocket.OPEN))
@@ -94,6 +97,7 @@ export class SystemAccessPoint extends EventEmitter {
9497
this.logger.log("keepalive timer expired, sending ping message...");
9598
this.webSocket.ping();
9699
});
100+
this.webSocketKeepaliveTimerReset$.next();
97101
}
98102

99103
/**
@@ -144,6 +148,9 @@ export class SystemAccessPoint extends EventEmitter {
144148
webSocket.on("ping", (data: Buffer) => {
145149
this.emit("websocket-ping", data);
146150
this.logger.debug("Ping received", data.toString("ascii"));
151+
this.webSocketKeepaliveTimerReset$.next();
152+
webSocket.pong(data);
153+
this.logger.debug("Pong sent", data.toString("ascii"));
147154
});
148155
webSocket.on("pong", (data: Buffer) => {
149156
this.emit("websocket-pong", data);
@@ -164,6 +171,8 @@ export class SystemAccessPoint extends EventEmitter {
164171
webSocket.on("close", (code, reason) => {
165172
this.emit("websocket-close", code, reason);
166173
this.logger.log("Connection closed");
174+
this.webSocketKeepaliveSubscription?.unsubscribe();
175+
this.webSocketKeepaliveSubscription = undefined;
167176
});
168177
webSocket.on("message", (data: RawData, isBinary: boolean) => {
169178
this.emit("websocket-message", data, isBinary);
@@ -182,8 +191,6 @@ export class SystemAccessPoint extends EventEmitter {
182191
throw new Error("Web socket is not open");
183192
}
184193

185-
this.webSocketKeepaliveSubscription?.unsubscribe();
186-
this.webSocketKeepaliveSubscription = undefined;
187194
if (force) {
188195
this.webSocket.terminate();
189196
} else {

test/system-access-point.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ describe("System Access Point", () => {
173173

174174
it("should call web socket event handlers", () => {
175175
spyOn(WebSocket.prototype, "send");
176+
spyOn(WebSocket.prototype, "pong");
176177
const sysAp = new SystemAccessPoint(
177178
"ap",
178179
"username",

0 commit comments

Comments
 (0)