diff --git a/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx b/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx index 4ae3c375..ca9dbbb6 100644 --- a/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx +++ b/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx @@ -18,28 +18,24 @@ export const DockerLogsId: React.FC = ({ }) => { const [term, setTerm] = React.useState(); const [lines, setLines] = React.useState(40); - const wsRef = useRef(null); + const wsRef = useRef(null); // Ref to hold WebSocket instance useEffect(() => { - if (containerId === "select-a-container") { - return; - } - - if ( - wsRef.current && - (wsRef.current.readyState === WebSocket.OPEN || - wsRef.current.readyState === WebSocket.CONNECTING) - ) { - wsRef.current.close(); - wsRef.current = null; - } - + // if (containerId === "select-a-container") { + // return; + // } const container = document.getElementById(id); if (container) { container.innerHTML = ""; } - // Crear nueva instancia de Terminal + if (wsRef.current) { + console.log(wsRef.current); + if (wsRef.current.readyState === WebSocket.OPEN) { + wsRef.current.close(); + } + wsRef.current = null; + } const termi = new Terminal({ cursorBlink: true, cols: 80, @@ -49,6 +45,7 @@ export const DockerLogsId: React.FC = ({ fontSize: 14, fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace', + convertEol: true, theme: { cursor: "transparent", @@ -57,10 +54,10 @@ export const DockerLogsId: React.FC = ({ }); const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; + const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?containerId=${containerId}&tail=${lines}${serverId ? `&serverId=${serverId}` : ""}`; const ws = new WebSocket(wsUrl); wsRef.current = ws; - const fitAddon = new FitAddon(); termi.loadAddon(fitAddon); // @ts-ignore @@ -79,17 +76,17 @@ export const DockerLogsId: React.FC = ({ ws.onclose = (e) => { console.log(e.reason); + termi.write(`Connection closed!\nReason: ${e.reason}\n`); wsRef.current = null; }; - return () => { if (wsRef.current?.readyState === WebSocket.OPEN) { - wsRef.current.close(); + ws.close(); + wsRef.current = null; } - wsRef.current = null; }; - }, [containerId, lines]); + }, [lines, containerId]); useEffect(() => { term?.clear(); diff --git a/apps/dokploy/server/wss/docker-container-logs.ts b/apps/dokploy/server/wss/docker-container-logs.ts index d704467c..63a0b89e 100644 --- a/apps/dokploy/server/wss/docker-container-logs.ts +++ b/apps/dokploy/server/wss/docker-container-logs.ts @@ -5,11 +5,6 @@ import { Client } from "ssh2"; import { WebSocketServer } from "ws"; import { getShell } from "./utils"; -function heartbeat() { - // @ts-ignore - this.isAlive = true; -} - export const setupDockerContainerLogsWebSocketServer = ( server: http.Server, ) => { @@ -48,11 +43,6 @@ export const setupDockerContainerLogsWebSocketServer = ( ws.close(); return; } - - // @ts-ignore - ws.isAlive = true; - ws.on("error", console.error); - ws.on("pong", heartbeat); try { if (serverId) { const server = await findServerById(serverId); @@ -148,18 +138,4 @@ export const setupDockerContainerLogsWebSocketServer = ( ws.send(errorMessage); } }); - - const interval = setInterval(function ping() { - for (const ws of wssTerm.clients) { - // @ts-ignore - if (ws.isAlive === false) return ws.terminate(); - // @ts-ignore - ws.isAlive = false; - ws.ping(); - } - }, 30000); - - wssTerm.on("close", function close() { - clearInterval(interval); - }); };