-
Notifications
You must be signed in to change notification settings - Fork 169
/
vnc-client.js
67 lines (58 loc) · 1.71 KB
/
vnc-client.js
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
// @flow
/* Global Module */
import RFB from '@novnc/novnc/core/rfb';
import type { ConsoleClientType } from './guests-console-types';
class VncClient implements ConsoleClientType {
rfb: Object;
canvasId: string;
socketUrl: string;
connected: Function;
disconnected: Function;
askPassword: Function;
constructor(canvasId: string, socketUrl: string, connected: Function, disconnected: Function, askPassword: Function) {
this.canvasId = canvasId;
this.socketUrl = socketUrl;
this.connected = connected;
this.disconnected = disconnected;
this.askPassword = askPassword;
}
connect = () => {
if (this.canvasId != null && this.socketUrl != null) {
const rfb = new RFB(document.getElementById(this.canvasId), this.socketUrl);
rfb.addEventListener('connect', this.onConnect);
rfb.addEventListener('disconnect', (e) => {
const error = e.detail.clean ? undefined : t('Something went wrong, connection is closed');
if (this.disconnected != null) {
this.disconnected(error);
}
});
rfb.addEventListener('credentialsrequired',
() => {
if (this.askPassword != null) {
this.askPassword().then(password => rfb.sendCredentials({ password }));
}
});
rfb.scaleViewport = false;
rfb.resizeSession = false;
this.rfb = rfb;
}
}
onConnect = () => {
if (this.rfb != null) {
this.rfb.focus();
if (this.connected != null) {
this.connected();
}
}
}
toggleScale = (expanded: boolean) => {
if (this.rfb != null) {
this.rfb.scaleViewport = expanded;
this.rfb.resizeSession = expanded;
}
}
canResize = true;
}
export {
VncClient,
};