Skip to content

Commit bec995c

Browse files
authored
Merge pull request #112 from sunu/retry-initial-connection
Retry a few times when the initial connection fails
2 parents d41bca2 + 975d084 commit bec995c

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

js/index.js

+48-29
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-
);
51+
let retryCount = 0;
4552

46-
// Add listeners to important events from the RFB module
47-
rfb.addEventListener("connect", connectedToServer);
48-
rfb.addEventListener("disconnect", disconnectedFromServer);
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+
);
4960

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

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

56-
// Clipboard
57-
function clipboardReceive(e) {
58-
document.getElementById("clipboard-text").value = e.detail.text;
59-
}
60-
rfb.addEventListener("clipboard", clipboardReceive);
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);
6176

62-
function clipboardSend() {
63-
const text = document.getElementById("clipboard-text").value;
64-
rfb.clipboardPasteFrom(text);
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);
69-
70-
setupTooltip(
71-
document.getElementById("clipboard-button"),
72-
document.getElementById("clipboard-container"),
73-
);
90+
91+
// Start the connection
92+
connect();

0 commit comments

Comments
 (0)