Skip to content

Commit

Permalink
fix(config): added configurable retry duration and logging (roerohan#11)
Browse files Browse the repository at this point in the history
* fix(config): added configurable retry duration and logging

fix roerohan#3

* fix(logging): logger functions fixed

* fix(demo): rebuild demo app on branch main
  • Loading branch information
roerohan authored Sep 26, 2021
1 parent f11ac67 commit 25c2e4e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Deploy to github.io

on:
push:
branches: [ master ]
branches: [ main ]

jobs:
deploy:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ interface Props {
background?: string;
qualityLevel?: number;
compressionLevel?: number;
retryDuration?: number; // in milliseconds
debug?: boolean; // show logs in the console
}
```

Expand Down
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function App() {
width: '75vw',
height: '75vh',
}}
debug
/>
)
: <div>VNC URL not provided.</div>
Expand Down
95 changes: 54 additions & 41 deletions src/lib/VncScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface Props {
background?: string;
qualityLevel?: number;
compressionLevel?: number;
retryDuration?: number;
debug?: boolean;
}

export default function VncScreen(props: Props) {
Expand All @@ -36,8 +38,16 @@ export default function VncScreen(props: Props) {
background,
qualityLevel,
compressionLevel,
retryDuration = 3000,
debug = false,
} = props;

const logger = {
log: (...args: any[]) => { if (debug) console.log(...args); },
info: (...args: any[]) => { if (debug) console.info(...args); },
error: (...args: any[]) => { if (debug) console.error(...args); },
};

const disconnect = () => {
if (!rfb) {
return;
Expand All @@ -48,48 +58,51 @@ export default function VncScreen(props: Props) {
};

const connect = () => {
disconnect();

if (!screen.current) {
return;
try {
disconnect();

if (!screen.current) {
return;
}

screen.current.innerHTML = '';

const _rfb = new RFB(screen.current, url);

_rfb.viewOnly = viewOnly || false;
_rfb.focusOnClick = focusOnClick || false;
_rfb.clipViewport = clipViewport || false;
_rfb.dragViewport = dragViewport || false;
_rfb.resizeSession = resizeSession || false;
_rfb.scaleViewport = scaleViewport || false;
_rfb.showDotCursor = showDotCursor || false;
_rfb.background = background || '';
_rfb.qualityLevel = qualityLevel || 6;
_rfb.compressionLevel = compressionLevel || 2;
setRfb(_rfb);

_rfb.addEventListener('connect', () => {
logger.info('Connected to remote VNC.');
setLoading(false);
});

_rfb.addEventListener('disconnect', () => {
logger.info(`Disconnected from remote VNC, retrying in ${retryDuration / 1000} seconds.`);
setTimeout(connect, retryDuration);
setLoading(true);
});

_rfb.addEventListener('credentialsrequired', () => {
const password = prompt("Password Required:");
_rfb.sendCredentials({ password: password });
});

_rfb.addEventListener('desktopname', (e: { detail: { name: string } }) => {
logger.info(`Desktop name is ${e.detail.name}`);
});
} catch (err) {
logger.error(err);
}

screen.current.innerHTML = '';

const _rfb = new RFB(screen.current, url);

_rfb.viewOnly = viewOnly || false;
_rfb.focusOnClick = focusOnClick || false;
_rfb.clipViewport = clipViewport || false;
_rfb.dragViewport = dragViewport || false;
_rfb.resizeSession = resizeSession || false;
_rfb.scaleViewport = scaleViewport || false;
_rfb.showDotCursor = showDotCursor || false;
_rfb.background = background || '';
_rfb.qualityLevel = qualityLevel || 6;
_rfb.compressionLevel = compressionLevel || 2;
setRfb(_rfb);

_rfb.addEventListener('connect', () => {
console.info('Connected to remote VNC.');
setLoading(false);
});

_rfb.addEventListener('disconnect', () => {
const retryDuration = 3000;
console.info(`Disconnected from remote VNC, retrying in ${retryDuration / 1000} seconds.`);
setTimeout(connect, retryDuration);
setLoading(true);
});

_rfb.addEventListener('credentialsrequired', () => {
const password = prompt("Password Required:");
_rfb.sendCredentials({ password: password });
});

_rfb.addEventListener('desktopname', (e: { detail: { name: string } }) => {
console.info(`Desktop name is ${e.detail.name}`);
});
};

useEffect(() => {
Expand Down

0 comments on commit 25c2e4e

Please sign in to comment.