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

Adds signal socket noop keepalive #41

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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