roerohan / react-vnc

A React Component to connect to a websockified VNC client using noVNC.
https://roerohan.github.io/react-vnc/
MIT License
88 stars 21 forks source link

Rebooting or Shutting Down Host #25

Closed candiedoperation closed 2 years ago

candiedoperation commented 2 years ago

I do have a brief idea of implementing the disconnect method by forwardRef() but I do not find a way to implement the RFB.machineReboot() or RFB.machineShutdown() functions listed in the noVNC API.md

image

roerohan commented 2 years ago

Hey @candiedoperation

Currently, there are no methods in react-vnc which implement RFB.machineReboot() or RFB.machineShutdown(). However, the rfb object from noVNC is exposed when the 'connect' event is fired. So, you can do something like:

<VncScreen
  url={vncUrl}
  scaleViewport
  debug
  ref={vncScreenRef}
  onConnect={(rfb) => {
    console.log('connected', rfb);
  }}
/>

You can use a ref to set the RFB object to your component scope.

function App() {
  const vncRfb = useRef(null);
  const vncScreenRef = useRef<React.ElementRef<typeof VncScreen>>(null);

  return (
    <VncScreen
      url={vncUrl}
      scaleViewport
      debug
      ref={vncScreenRef}
      onConnect={(rfb) => {
        console.log('connected', rfb);
        vncRfb.current = rfb;
      }}
    />
  )
}

You can use this rfb object to run any method that is described in the noVNC API.

roerohan commented 2 years ago

In #26, I've exposed the rfb object. So, if you have a code snippet like:

function App() {
  const vncScreenRef = useRef<React.ElementRef<typeof VncScreen>>(null);

  return (
    <VncScreen
      url={vncUrl}
      scaleViewport
      debug
      ref={vncScreenRef}
    />
  )
}

You can access the rfb object using vncScreenRef.current?.rfb. If the rfb object isn't set, it's value is null.

roerohan commented 2 years ago

In #28 , I've also added support for the following methods from RFB

sendCredentials,
sendKey,
sendCtrlAltDel,
focus,
blur,
machineShutdown,
machineReboot,
machineReset,
clipboardPaste

So, you could just do vncScreenRef.current?.machineShutdown() from the above example.