-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
VncScreen.tsx
149 lines (125 loc) · 3.95 KB
/
VncScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { useEffect, useRef, useState } from 'react';
import RFB from '../noVNC/core/rfb';
interface Props {
url: string;
style?: object;
className?: string;
viewOnly?: boolean;
focusOnClick?: boolean;
clipViewport?: boolean;
dragViewport?: boolean;
scaleViewport?: boolean;
resizeSession?: boolean;
showDotCursor?: boolean;
background?: string;
qualityLevel?: number;
compressionLevel?: number;
retryDuration?: number;
debug?: boolean;
}
export default function VncScreen(props: Props) {
const [rfb, setRfb] = useState<RFB | null>(null);
const screen = useRef<HTMLDivElement>(null);
const [loading, setLoading] = useState<boolean>(true);
const {
url,
style,
className,
viewOnly,
focusOnClick,
clipViewport,
dragViewport,
scaleViewport,
resizeSession,
showDotCursor,
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;
}
rfb.disconnect();
setRfb(null);
};
const connect = () => {
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);
}
};
useEffect(() => {
connect();
return disconnect;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const handleClick = () => {
if (!rfb) return;
rfb.focus();
}
const handleMouseEnter = () => {
if (document.activeElement && document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
handleClick();
};
const handleMouseLeave = () => {
if (!rfb) {
return;
}
rfb.blur();
};
return (
<>
<div
style={style}
className={className}
ref={screen}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
/>
{loading && <div className="text-white loading">Loading...</div>}
</>
);
}