Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically reconnect if we lose connection #81

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 60 additions & 39 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,8 @@ import RFB from "@novnc/novnc/core/rfb";

import { setupTooltip } from "./tooltip.js";

// When this function is called we have successfully connected to a server
function connectedToServer() {
status("Connected");
}

// This function is called when we are disconnected
function disconnectedFromServer(e) {
if (e.detail.clean) {
status("Disconnected");
} else {
status("Something went wrong, connection is closed");
}
}

// Show a status text in the top bar
function status(text) {
function setStatusText(text) {
document.getElementById("status").textContent = text;
}

Expand All @@ -36,36 +22,71 @@ function status(text) {
let websockifyUrl = new URL("../desktop-websockify/", window.location);
websockifyUrl.protocol = window.location.protocol === "https:" ? "wss" : "ws";

// Creating a new RFB object will start a new connection
const rfb = new RFB(
document.getElementById("screen"),
websockifyUrl.toString(),
{},
);
/**
* Setup two way clipboard sync with the given rfb
*
* @param {RFB} rfb
*/
function setupClipboardSync(rfb) {
const clipboardText = document.getElementById("clipboard-text");

// Add listeners to important events from the RFB module
rfb.addEventListener("connect", connectedToServer);
rfb.addEventListener("disconnect", disconnectedFromServer);
// Listen for clipboard events on the remote system, and automatically
// update our local textarea with those values
rfb.addEventListener("clipboard", (e) => {
clipboardText.value = e.detail.text;
});

// Scale our viewport so the user doesn't have to scroll
rfb.scaleViewport = true;
const clipboardClientChange = () => {
const text = clipboardText.value;
rfb.clipboardPasteFrom(text);
};
rfb.addEventListener("connect", () => {
console.log("connecting clipboard sync");
clipboardText.addEventListener("change", clipboardClientChange);
});
rfb.addEventListener("disconnect", () => {
console.log("disconnecting clipboard sync");
clipboardText.removeEventListener("change", clipboardClientChange);
});
}

// Use a CSS variable to set background color
rfb.background = "var(--jupyter-medium-dark-grey)";
function connect() {
// Creating a new RFB object will start a new connection
const rfb = new RFB(
document.getElementById("screen"),
websockifyUrl.toString(),
{},
);

// Clipboard
function clipboardReceive(e) {
document.getElementById("clipboard-text").value = e.detail.text;
}
rfb.addEventListener("clipboard", clipboardReceive);
// Update status when connection is made or broken
rfb.addEventListener("connect", () => {
console.log("connected");
setStatusText("Connected");
});
rfb.addEventListener("disconnect", () => {
console.log("disconnected");
let countDown = 5;
setStatusText(`Reconnecting in ${countDown}s`);
const intervalHandle = setInterval(() => {
countDown -= 1;
setStatusText(`Reconnecting in ${countDown}s`);
console.log(countDown);
if (countDown === 0) {
clearInterval(intervalHandle);
connect();
}
}, 1000);
Comment on lines +68 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could make sense to extract into a reconnect / attemptReconnect / handleDisconnect function or similar to improve readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

function clipboardSend() {
const text = document.getElementById("clipboard-text").value;
rfb.clipboardPasteFrom(text);
// Scale our viewport so the user doesn't have to scroll
rfb.scaleViewport = true;

// Use a CSS variable to set background color
rfb.background = "var(--jupyter-medium-dark-grey)";
setupClipboardSync(rfb);
}
document
.getElementById("clipboard-text")
.addEventListener("change", clipboardSend);

connect();

setupTooltip(
document.getElementById("clipboard-button"),
Expand Down
Loading