Fix VNC connection
This commit is contained in:
		@@ -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]));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,25 +44,18 @@ 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 (
 | 
				
			||||||
 | 
					    <AsyncWidget
 | 
				
			||||||
 | 
					      loadKey={counter.current}
 | 
				
			||||||
 | 
					      load={load}
 | 
				
			||||||
 | 
					      ready={url !== undefined && counter.current}
 | 
				
			||||||
 | 
					      errMsg="Failed to get a token to initialize VNC connection!"
 | 
				
			||||||
 | 
					      build={() => (
 | 
				
			||||||
        <div
 | 
					        <div
 | 
				
			||||||
          style={{
 | 
					          style={{
 | 
				
			||||||
            display: "flex",
 | 
					            display: "flex",
 | 
				
			||||||
@@ -73,14 +65,14 @@ function VNCInner(p: { vm: VMInfo }): React.ReactElement {
 | 
				
			|||||||
          }}
 | 
					          }}
 | 
				
			||||||
        >
 | 
					        >
 | 
				
			||||||
          <VncScreen
 | 
					          <VncScreen
 | 
				
			||||||
        url={urlRef.current}
 | 
					            url={url!}
 | 
				
			||||||
            onDisconnect={() => {
 | 
					            onDisconnect={() => {
 | 
				
			||||||
          console.info("VNC disconnected " + urlRef.current);
 | 
					              console.info("VNC disconnected " + url);
 | 
				
			||||||
          urlRef.current = undefined;
 | 
					              reconnect();
 | 
				
			||||||
          load();
 | 
					 | 
				
			||||||
          setURL(undefined);
 | 
					 | 
				
			||||||
            }}
 | 
					            }}
 | 
				
			||||||
          />
 | 
					          />
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					      )}
 | 
				
			||||||
 | 
					    />
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user