mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-02-04 12:40:08 +00:00
fix: websocket
This commit is contained in:
@@ -63,6 +63,15 @@ const arraysEqual = (a: Uint8Array, b: Uint8Array) => {
|
|||||||
const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl, ...props }) => {
|
const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl, ...props }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const fmRef = useRef<HTMLDivElement>(null);
|
const fmRef = useRef<HTMLDivElement>(null);
|
||||||
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (wsRef.current) {
|
||||||
|
wsRef.current.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
const [dOpen, setdOpen] = useState(false);
|
const [dOpen, setdOpen] = useState(false);
|
||||||
const [uOpen, setuOpen] = useState(false);
|
const [uOpen, setuOpen] = useState(false);
|
||||||
@@ -175,10 +184,14 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
|
|||||||
|
|
||||||
const [currentPath, setPath] = useState('');
|
const [currentPath, setPath] = useState('');
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
|
||||||
listFile();
|
listFile();
|
||||||
}, [currentPath])
|
}
|
||||||
|
}, [wsRef.current, currentPath])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const ws = new WebSocket(wsUrl);
|
const ws = new WebSocket(wsUrl);
|
||||||
|
wsRef.current = ws;
|
||||||
ws.binaryType = 'arraybuffer';
|
ws.binaryType = 'arraybuffer';
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
listFile();
|
listFile();
|
||||||
@@ -229,8 +242,9 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
|
|||||||
if (uOpen) setuOpen(false);
|
if (uOpen) setuOpen(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}, [wsUrl])
|
||||||
|
|
||||||
const listFile = () => {
|
let listFile = () => {
|
||||||
const prefix = new Int8Array([FMOpcode.List]);
|
const prefix = new Int8Array([FMOpcode.List]);
|
||||||
const pathMsg = new TextEncoder().encode(currentPath);
|
const pathMsg = new TextEncoder().encode(currentPath);
|
||||||
|
|
||||||
@@ -238,7 +252,7 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
|
|||||||
msg.set(prefix);
|
msg.set(prefix);
|
||||||
msg.set(pathMsg, prefix.length);
|
msg.set(pathMsg, prefix.length);
|
||||||
|
|
||||||
ws.send(msg);
|
wsRef.current?.send(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
const downloadFile = (basename: string) => {
|
const downloadFile = (basename: string) => {
|
||||||
@@ -250,7 +264,7 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
|
|||||||
msg.set(prefix);
|
msg.set(prefix);
|
||||||
msg.set(filePathMessage, prefix.length);
|
msg.set(filePathMessage, prefix.length);
|
||||||
|
|
||||||
ws.send(msg);
|
wsRef.current?.send(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploadFile = async (file: File) => {
|
const uploadFile = async (file: File) => {
|
||||||
@@ -259,13 +273,13 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
|
|||||||
|
|
||||||
// Send header
|
// Send header
|
||||||
const header = fm.buildUploadHeader({ path: currentPath, file: file });
|
const header = fm.buildUploadHeader({ path: currentPath, file: file });
|
||||||
ws.send(header);
|
wsRef.current?.send(header);
|
||||||
|
|
||||||
// Send data chunks
|
// Send data chunks
|
||||||
while (offset < file.size) {
|
while (offset < file.size) {
|
||||||
const chunk = file.slice(offset, offset + chunkSize);
|
const chunk = file.slice(offset, offset + chunkSize);
|
||||||
const arrayBuffer = await fm.readFileAsArrayBuffer(chunk);
|
const arrayBuffer = await fm.readFileAsArrayBuffer(chunk);
|
||||||
if (arrayBuffer) ws.send(arrayBuffer);
|
if (arrayBuffer) wsRef.current?.send(arrayBuffer);
|
||||||
offset += chunkSize;
|
offset += chunkSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,8 +28,19 @@ interface XtermProps {
|
|||||||
|
|
||||||
const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ wsUrl, setClose, ...props }) => {
|
const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ wsUrl, setClose, ...props }) => {
|
||||||
const terminalRef = useRef<HTMLDivElement>(null);
|
const terminalRef = useRef<HTMLDivElement>(null);
|
||||||
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (wsRef.current) {
|
||||||
|
wsRef.current.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const ws = new WebSocket(wsUrl);
|
const ws = new WebSocket(wsUrl);
|
||||||
|
wsRef.current = ws;
|
||||||
ws.binaryType = "arraybuffer";
|
ws.binaryType = "arraybuffer";
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
onResize();
|
onResize();
|
||||||
@@ -44,6 +55,7 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
|
|||||||
description: "View console for details.",
|
description: "View console for details.",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}, [wsUrl]);
|
||||||
|
|
||||||
const terminal = useRef(
|
const terminal = useRef(
|
||||||
new Terminal({
|
new Terminal({
|
||||||
@@ -73,7 +85,7 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
|
|||||||
msg.set(prefix);
|
msg.set(prefix);
|
||||||
msg.set(resizeMessage, prefix.length);
|
msg.set(resizeMessage, prefix.length);
|
||||||
|
|
||||||
ws.send(msg);
|
wsRef.current?.send(msg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -92,20 +104,19 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ws || !terminalRef.current) return;
|
if (!wsRef.current || !terminalRef.current) return;
|
||||||
|
const attachAddon = new AttachAddon(wsRef.current);
|
||||||
const attachAddon = new AttachAddon(ws);
|
|
||||||
terminal.loadAddon(attachAddon);
|
terminal.loadAddon(attachAddon);
|
||||||
terminal.loadAddon(fitAddon);
|
terminal.loadAddon(fitAddon);
|
||||||
terminal.open(terminalRef.current);
|
terminal.open(terminalRef.current);
|
||||||
window.addEventListener('resize', onResize);
|
window.addEventListener('resize', onResize);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('resize', onResize);
|
window.removeEventListener('resize', onResize);
|
||||||
if (ws) {
|
if (wsRef.current) {
|
||||||
ws.close();
|
wsRef.current.close();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [ws, terminal]);
|
}, [wsRef.current, terminal]);
|
||||||
|
|
||||||
return <div ref={terminalRef} {...props} />;
|
return <div ref={terminalRef} {...props} />;
|
||||||
};
|
};
|
||||||
@@ -116,7 +127,6 @@ export const TerminalPage = () => {
|
|||||||
|
|
||||||
const { id } = useParams<{ id: string }>();
|
const { id } = useParams<{ id: string }>();
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const fetchTerminal = async () => {
|
const fetchTerminal = async () => {
|
||||||
if (id && !terminal) {
|
if (id && !terminal) {
|
||||||
try {
|
try {
|
||||||
@@ -130,7 +140,9 @@ export const TerminalPage = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
fetchTerminal();
|
fetchTerminal();
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user