Skip to content

Commit 8c1d4f2

Browse files
committed
retry a few time if a connection fails
Sometimes the initial websocket connection fails. This is a workaround that retries the connection a few times with some delay in between connection attempts.
1 parent d41bca2 commit 8c1d4f2

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

js/index.js

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import RFB from "@novnc/novnc/core/rfb";
1111

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

14+
const maxRetryCount = 5;
15+
const retryInterval = 3; // seconds
16+
1417
// When this function is called we have successfully connected to a server
1518
function connectedToServer() {
1619
status("Connected");
@@ -22,6 +25,15 @@ function disconnectedFromServer(e) {
2225
status("Disconnected");
2326
} else {
2427
status("Something went wrong, connection is closed");
28+
if (retryCount < maxRetryCount) {
29+
status(`Reconnecting in ${retryInterval} seconds`);
30+
setTimeout(() => {
31+
connect();
32+
retryCount++;
33+
}, retryInterval * 1000);
34+
} else {
35+
status("Failed to connect, giving up");
36+
}
2537
}
2638
}
2739

@@ -36,38 +48,45 @@ function status(text) {
3648
let websockifyUrl = new URL("../desktop-websockify/", window.location);
3749
websockifyUrl.protocol = window.location.protocol === "https:" ? "wss" : "ws";
3850

39-
// Creating a new RFB object will start a new connection
40-
const rfb = new RFB(
41-
document.getElementById("screen"),
42-
websockifyUrl.toString(),
43-
{},
44-
);
45-
46-
// Add listeners to important events from the RFB module
47-
rfb.addEventListener("connect", connectedToServer);
48-
rfb.addEventListener("disconnect", disconnectedFromServer);
49-
50-
// Scale our viewport so the user doesn't have to scroll
51-
rfb.scaleViewport = true;
52-
53-
// Use a CSS variable to set background color
54-
rfb.background = "var(--jupyter-medium-dark-grey)";
51+
let retryCount = 0;
5552

56-
// Clipboard
57-
function clipboardReceive(e) {
58-
document.getElementById("clipboard-text").value = e.detail.text;
59-
}
60-
rfb.addEventListener("clipboard", clipboardReceive);
61-
62-
function clipboardSend() {
63-
const text = document.getElementById("clipboard-text").value;
64-
rfb.clipboardPasteFrom(text);
53+
function connect() {
54+
// Creating a new RFB object will start a new connection
55+
let rfb = new RFB(
56+
document.getElementById("screen"),
57+
websockifyUrl.toString(),
58+
{},
59+
);
60+
61+
// Add listeners to important events from the RFB module
62+
rfb.addEventListener("connect", connectedToServer);
63+
rfb.addEventListener("disconnect", disconnectedFromServer);
64+
65+
// Scale our viewport so the user doesn't have to scroll
66+
rfb.scaleViewport = true;
67+
68+
// Use a CSS variable to set background color
69+
rfb.background = "var(--jupyter-medium-dark-grey)";
70+
71+
// Clipboard
72+
function clipboardReceive(e) {
73+
document.getElementById("clipboard-text").value = e.detail.text;
74+
}
75+
rfb.addEventListener("clipboard", clipboardReceive);
76+
77+
function clipboardSend() {
78+
const text = document.getElementById("clipboard-text").value;
79+
rfb.clipboardPasteFrom(text);
80+
}
81+
document
82+
.getElementById("clipboard-text")
83+
.addEventListener("change", clipboardSend);
84+
85+
setupTooltip(
86+
document.getElementById("clipboard-button"),
87+
document.getElementById("clipboard-container"),
88+
);
6589
}
66-
document
67-
.getElementById("clipboard-text")
68-
.addEventListener("change", clipboardSend);
6990

70-
setupTooltip(
71-
document.getElementById("clipboard-button"),
72-
document.getElementById("clipboard-container"),
73-
);
91+
// Start the connection
92+
connect();

0 commit comments

Comments
 (0)