mirror of
https://github.com/penpot/penpot.git
synced 2026-05-02 06:38:20 +00:00
🐛 Send a keep alive message in websocket connection
This commit is contained in:
parent
698852cbeb
commit
03d0c62de1
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user