From 84009c5e9b7b576bfa06b700cb0f4604073c54cc Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 5 Oct 2024 16:04:28 -0600 Subject: [PATCH] refactor: add close websocket --- .../deployments/show-deployment.tsx | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/dokploy/components/dashboard/application/deployments/show-deployment.tsx b/apps/dokploy/components/dashboard/application/deployments/show-deployment.tsx index 2e892523..19b01e80 100644 --- a/apps/dokploy/components/dashboard/application/deployments/show-deployment.tsx +++ b/apps/dokploy/components/dashboard/application/deployments/show-deployment.tsx @@ -16,6 +16,7 @@ interface Props { export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => { const [data, setData] = useState(""); const endOfLogsRef = useRef(null); + const wsRef = useRef(null); // Ref to hold WebSocket instance useEffect(() => { if (!open || !logPath) return; @@ -24,12 +25,25 @@ export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => { const wsUrl = `${protocol}//${window.location.host}/listen-deployment?logPath=${logPath}${serverId ? `&serverId=${serverId}` : ""}`; const ws = new WebSocket(wsUrl); + wsRef.current = ws; // Store WebSocket instance in ref ws.onmessage = (e) => { setData((currentData) => currentData + e.data); }; - return () => ws.close(); + ws.onerror = (error) => { + console.error("WebSocket error: ", error); + }; + + ws.onclose = () => { + console.log("WebSocket connection closed"); + wsRef.current = null; // Clear reference on close + }; + + return () => { + ws.close(); + wsRef.current = null; + }; }, [logPath, open]); const scrollToBottom = () => { @@ -46,6 +60,11 @@ export const ShowDeployment = ({ logPath, open, onClose, serverId }: Props) => { onOpenChange={(e) => { onClose(); if (!e) setData(""); + + if (!e && wsRef.current) { + wsRef.current.close(); // Close WebSocket when modal closes + setData(""); + } }} >