Skip to content

Commit

Permalink
Merge pull request #41 from whereby/geirbakke/noop-keepalive
Browse files Browse the repository at this point in the history
Adds signal socket noop keepalive
  • Loading branch information
geirbakke committed Nov 28, 2023
2 parents 7bab7f2 + 70ab0d2 commit 429067d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@whereby/jslib-media",
"description": "Media library for Whereby",
"version": "1.5.1",
"version": "1.5.2",
"private": false,
"license": "MIT",
"homepage": "https://github.com/whereby/jslib-media",
Expand Down
20 changes: 20 additions & 0 deletions src/utils/ServerSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { PROTOCOL_RESPONSES } from "../model/protocol";

const DEFAULT_SOCKET_PATH = "/protocol/socket.io/v4";

const NOOP_KEEPALIVE_INTERVAL = 2000;

/**
* Wrapper class that extends the Socket.IO client library.
*/
Expand Down Expand Up @@ -42,6 +44,24 @@ export default class ServerSocket {
const transport = this.getTransport();
if (transport === "websocket") {
this._wasConnectedUsingWebsocket = true;

// start noop keepalive loop to detect client side disconnects fast
if (!this.noopKeepaliveInterval)
this.noopKeepaliveInterval = setInterval(() => {
try {
// send a noop message if it thinks it is connected (might not be)
if (this._socket.connected) {
this._socket.io.engine.sendPacket("noop");
}
} catch (ex) {}
}, NOOP_KEEPALIVE_INTERVAL);
}
});

this._socket.on("disconnect", () => {
if (this.noopKeepaliveInterval) {
clearInterval(this.noopKeepaliveInterval);
this.noopKeepaliveInterval = null;
}
});
}
Expand Down

0 comments on commit 429067d

Please sign in to comment.