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

Retry a few times when the initial connection fails #112

Merged
merged 2 commits into from
Jun 12, 2024
Merged
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
77 changes: 48 additions & 29 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import RFB from "@novnc/novnc/core/rfb";

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

const maxRetryCount = 5;
const retryInterval = 3; // seconds

// When this function is called we have successfully connected to a server
function connectedToServer() {
status("Connected");
Expand All @@ -22,6 +25,15 @@ function disconnectedFromServer(e) {
status("Disconnected");
} else {
status("Something went wrong, connection is closed");
if (retryCount < maxRetryCount) {
status(`Reconnecting in ${retryInterval} seconds`);
setTimeout(() => {
connect();
retryCount++;
}, retryInterval * 1000);
} else {
status("Failed to connect, giving up");
}
}
}

Expand All @@ -36,38 +48,45 @@ 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(),
{},
);
let retryCount = 0;

// Add listeners to important events from the RFB module
rfb.addEventListener("connect", connectedToServer);
rfb.addEventListener("disconnect", disconnectedFromServer);
function connect() {
// Creating a new RFB object will start a new connection
let rfb = new RFB(
document.getElementById("screen"),
websockifyUrl.toString(),
{},
);

// Scale our viewport so the user doesn't have to scroll
rfb.scaleViewport = true;
// Add listeners to important events from the RFB module
rfb.addEventListener("connect", connectedToServer);
rfb.addEventListener("disconnect", disconnectedFromServer);

// Use a CSS variable to set background color
rfb.background = "var(--jupyter-medium-dark-grey)";
// Scale our viewport so the user doesn't have to scroll
rfb.scaleViewport = true;

// Clipboard
function clipboardReceive(e) {
document.getElementById("clipboard-text").value = e.detail.text;
}
rfb.addEventListener("clipboard", clipboardReceive);
// Use a CSS variable to set background color
rfb.background = "var(--jupyter-medium-dark-grey)";

// Clipboard
function clipboardReceive(e) {
document.getElementById("clipboard-text").value = e.detail.text;
}
rfb.addEventListener("clipboard", clipboardReceive);

function clipboardSend() {
const text = document.getElementById("clipboard-text").value;
rfb.clipboardPasteFrom(text);
function clipboardSend() {
const text = document.getElementById("clipboard-text").value;
rfb.clipboardPasteFrom(text);
}
document
.getElementById("clipboard-text")
.addEventListener("change", clipboardSend);

setupTooltip(
document.getElementById("clipboard-button"),
document.getElementById("clipboard-container"),
);
}
document
.getElementById("clipboard-text")
.addEventListener("change", clipboardSend);

setupTooltip(
document.getElementById("clipboard-button"),
document.getElementById("clipboard-container"),
);

// Start the connection
connect();