Fix VNC connection

This commit is contained in:
Pierre HUBERT 2023-10-18 18:36:24 +02:00
parent 197fbcf259
commit b007826710
2 changed files with 51 additions and 43 deletions

View File

@ -84,11 +84,27 @@ impl VNCActor {
let mut buff: [u8; 5000] = [0; 5000]; let mut buff: [u8; 5000] = [0; 5000];
loop { loop {
match read_half.read(&mut buff).await { match read_half.read(&mut buff).await {
Ok(l) => { Ok(mut l) => {
if l == 0 { if l == 0 {
log::error!("Got empty read. Closing read end..."); log::warn!("Got empty read!");
addr.do_send(CloseWebSocketReq);
return; // Ugly hack made to wait for next byte
let mut one_byte_buff: [u8; 1] = [0; 1];
match read_half.read_exact(&mut one_byte_buff).await {
Ok(b) => {
if b == 0 {
log::error!("Did not get a byte !");
let _ = addr.send(CloseWebSocketReq).await;
break;
}
buff[0] = one_byte_buff[0];
l = 1;
}
Err(e) => {
log::error!("Failed to read 1 BYTE from remote socket. Stopping now... {:?}", e);
break;
}
}
} }
let to_send = SendBytesReq(Vec::from(&buff[0..l])); let to_send = SendBytesReq(Vec::from(&buff[0..l]));

View File

@ -1,4 +1,3 @@
import { CircularProgress } from "@mui/material";
import React from "react"; import React from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { VncScreen } from "react-vnc"; import { VncScreen } from "react-vnc";
@ -28,15 +27,15 @@ export function VNCRoute(): React.ReactElement {
function VNCInner(p: { vm: VMInfo }): React.ReactElement { function VNCInner(p: { vm: VMInfo }): React.ReactElement {
const snackbar = useSnackbar(); const snackbar = useSnackbar();
const counter = React.useRef(false);
const [url, setURL] = React.useState<string | undefined>(); const [url, setURL] = React.useState<string | undefined>();
const urlRef = React.useRef<string | undefined>();
const load = async () => { const load = async () => {
try { try {
const u = await VMApi.OneShotVNCURL(p.vm); const u = await VMApi.OneShotVNCURL(p.vm);
console.info(u); console.info(u);
if (urlRef.current === undefined) { if (counter.current === false) {
urlRef.current = u; counter.current = true;
setURL(u); setURL(u);
} }
} catch (e) { } catch (e) {
@ -45,42 +44,35 @@ function VNCInner(p: { vm: VMInfo }): React.ReactElement {
} }
}; };
React.useEffect(() => { const reconnect = () => {
load(); counter.current = false;
}); setURL(undefined);
};
if (urlRef.current === undefined)
return (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100%",
}}
>
<CircularProgress />
</div>
);
return ( return (
<div <AsyncWidget
style={{ loadKey={counter.current}
display: "flex", load={load}
alignItems: "center", ready={url !== undefined && counter.current}
justifyContent: "center", errMsg="Failed to get a token to initialize VNC connection!"
height: "100%", build={() => (
}} <div
> style={{
<VncScreen display: "flex",
url={urlRef.current} alignItems: "center",
onDisconnect={() => { justifyContent: "center",
console.info("VNC disconnected " + urlRef.current); height: "100%",
urlRef.current = undefined; }}
load(); >
setURL(undefined); <VncScreen
}} url={url!}
/> onDisconnect={() => {
</div> console.info("VNC disconnected " + url);
reconnect();
}}
/>
</div>
)}
/>
); );
} }