fix(striker-ui): expose novnc internal websock error, close events

main^2
Tsu-ba-me 7 months ago
parent 5b12924e1f
commit 76219f135d
  1. 35
      striker-ui/components/Display/VncDisplay.tsx
  2. 4
      striker-ui/types/VncDisplay.d.ts
  3. 1
      striker-ui/types/novnc__novnc.d.ts

@ -1,4 +1,5 @@
import RFB from '@novnc/novnc/core/rfb'; import RFB from '@novnc/novnc/core/rfb';
import Websock from '@novnc/novnc/core/websock';
import { useEffect } from 'react'; import { useEffect } from 'react';
const rfbConnect: RfbConnectFunction = ({ const rfbConnect: RfbConnectFunction = ({
@ -9,6 +10,8 @@ const rfbConnect: RfbConnectFunction = ({
focusOnClick = false, focusOnClick = false,
onConnect, onConnect,
onDisconnect, onDisconnect,
onWsClose,
onWsError,
qualityLevel = 6, qualityLevel = 6,
resizeSession = true, resizeSession = true,
rfb, rfb,
@ -45,6 +48,23 @@ const rfbConnect: RfbConnectFunction = ({
if (onDisconnect) { if (onDisconnect) {
rfb.current.addEventListener('disconnect', onDisconnect); rfb.current.addEventListener('disconnect', onDisconnect);
} }
/* eslint-disable no-underscore-dangle */
const ws: typeof Websock = rfb.current._sock;
const socketClose = ws._eventHandlers.close;
const socketError = ws._eventHandlers.error;
ws.on('close', (e?: WebsockCloseEvent) => {
socketClose(e);
onWsClose?.call(null, e);
});
ws.on('error', (e: Event) => {
socketError(e);
onWsError?.call(null, e);
});
/* eslint-enable no-underscore-dangle */
}; };
const rfbDisconnect: RfbDisconnectFunction = (rfb) => { const rfbDisconnect: RfbDisconnectFunction = (rfb) => {
@ -58,6 +78,8 @@ const VncDisplay = (props: VncDisplayProps): JSX.Element => {
const { const {
onConnect, onConnect,
onDisconnect, onDisconnect,
onWsClose,
onWsError,
rfb, rfb,
rfbConnectArgs, rfbConnectArgs,
rfbScreen, rfbScreen,
@ -73,6 +95,8 @@ const VncDisplay = (props: VncDisplayProps): JSX.Element => {
const args: RfbConnectArgs = { const args: RfbConnectArgs = {
onConnect, onConnect,
onDisconnect, onDisconnect,
onWsClose,
onWsError,
rfb, rfb,
rfbScreen, rfbScreen,
url, url,
@ -83,7 +107,16 @@ const VncDisplay = (props: VncDisplayProps): JSX.Element => {
} else { } else {
rfbDisconnect(rfb); rfbDisconnect(rfb);
} }
}, [initUrl, onConnect, onDisconnect, rfb, rfbConnectArgs, rfbScreen]); }, [
initUrl,
onConnect,
onDisconnect,
onWsClose,
onWsError,
rfb,
rfbConnectArgs,
rfbScreen,
]);
useEffect( useEffect(
() => () => { () => () => {

@ -4,6 +4,8 @@ type RfbRef = import('react').MutableRefObject<
type RfbScreenRef = import('react').MutableRefObject<HTMLDivElement | null>; type RfbScreenRef = import('react').MutableRefObject<HTMLDivElement | null>;
type WebsockCloseEvent = Event & { code: number; reason: string };
type RfbConnectArgs = { type RfbConnectArgs = {
background?: string; background?: string;
clipViewport?: boolean; clipViewport?: boolean;
@ -12,6 +14,8 @@ type RfbConnectArgs = {
focusOnClick?: boolean; focusOnClick?: boolean;
onConnect?: () => void; onConnect?: () => void;
onDisconnect?: (event: { detail: { clean: boolean } }) => void; onDisconnect?: (event: { detail: { clean: boolean } }) => void;
onWsClose?: (event?: WebsockCloseEvent) => void;
onWsError?: (event: Event) => void;
qualityLevel?: number; qualityLevel?: number;
resizeSession?: boolean; resizeSession?: boolean;
rfb: RfbRef; rfb: RfbRef;

@ -1 +1,2 @@
declare module '@novnc/novnc/core/rfb'; declare module '@novnc/novnc/core/rfb';
declare module '@novnc/novnc/core/websock';

Loading…
Cancel
Save