37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
// Thin wrapper over the vendored guacamole-common-js client.
|
|
// The library attaches a global `Guacamole` object when loaded.
|
|
declare const Guacamole: any;
|
|
|
|
export function openConsole(
|
|
container: HTMLElement,
|
|
wsUrl: string,
|
|
connectData = ""
|
|
): { disconnect: () => void; setScale: (scale: number) => void } {
|
|
// Guacamole's WebSocketTunnel builds the socket URL as `wsUrl + "?" + data`,
|
|
// so wsUrl must NOT already contain a query string — pass params via connectData.
|
|
const tunnel = new Guacamole.WebSocketTunnel(wsUrl);
|
|
const client = new Guacamole.Client(tunnel);
|
|
|
|
container.innerHTML = "";
|
|
container.appendChild(client.getDisplay().getElement());
|
|
|
|
client.connect(connectData);
|
|
|
|
// Wire keyboard + mouse.
|
|
const mouse = new Guacamole.Mouse(client.getDisplay().getElement());
|
|
mouse.onmousedown = mouse.onmouseup = mouse.onmousemove = (state: any) =>
|
|
client.sendMouseState(state);
|
|
const keyboard = new Guacamole.Keyboard(document);
|
|
keyboard.onkeydown = (k: number) => client.sendKeyEvent(1, k);
|
|
keyboard.onkeyup = (k: number) => client.sendKeyEvent(0, k);
|
|
|
|
return {
|
|
disconnect() {
|
|
client.disconnect();
|
|
},
|
|
setScale(scale: number) {
|
|
client.getDisplay().scale(scale);
|
|
},
|
|
};
|
|
}
|