🐛 Send a keep alive message in websocket connection

This commit is contained in:
alonso.torres 2026-02-18 13:16:13 +01:00 committed by Alonso Torres
parent 698852cbeb
commit 03d0c62de1
2 changed files with 14 additions and 1 deletions

View File

@ -1,5 +1,7 @@
import "./style.css";
const KEEP_ALIVE_TIME = 30000; // 30 seconds
// get the current theme from the URL
const searchParams = new URLSearchParams(window.location.search);
document.body.dataset.theme = searchParams.get("theme") ?? "light";
@ -72,8 +74,12 @@ function connectToMcpServer(baseUrl?: string, token?: string): void {
};
ws.onmessage = (event) => {
console.log("Received from MCP server:", event.data);
try {
if (event.data === "keep-alive") {
// Keep alive response, ignore it
return;
}
console.log("Received from MCP server:", event.data);
const request = JSON.parse(event.data);
// Forward the task request to the plugin for execution
parent.postMessage(request, "*");
@ -82,8 +88,11 @@ function connectToMcpServer(baseUrl?: string, token?: string): void {
}
};
const interval = setInterval(() => ws?.send("keep-alive"), KEEP_ALIVE_TIME);
ws.onclose = (event: CloseEvent) => {
console.log("Disconnected from MCP server");
clearInterval(interval);
const message = event.reason || undefined;
updateConnectionStatus("disconnected", "Disconnected", false, message);
ws = null;

View File

@ -72,6 +72,10 @@ export class PluginBridge {
ws.on("message", (data: Buffer) => {
this.logger.debug("Received WebSocket message: %s", data.toString());
try {
if (data.toString() === "keep-alive") {
ws.send("keep-alive");
return;
}
const response: PluginTaskResponse<any> = JSON.parse(data.toString());
this.handlePluginTaskResponse(response);
} catch (error) {