@@ -11,6 +11,9 @@ import RFB from "@novnc/novnc/core/rfb";
11
11
12
12
import { setupTooltip } from "./tooltip.js" ;
13
13
14
+ const maxRetryCount = 5 ;
15
+ const retryInterval = 3 ; // seconds
16
+
14
17
// When this function is called we have successfully connected to a server
15
18
function connectedToServer ( ) {
16
19
status ( "Connected" ) ;
@@ -22,6 +25,15 @@ function disconnectedFromServer(e) {
22
25
status ( "Disconnected" ) ;
23
26
} else {
24
27
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
+ }
25
37
}
26
38
}
27
39
@@ -36,38 +48,45 @@ function status(text) {
36
48
let websockifyUrl = new URL ( "../desktop-websockify/" , window . location ) ;
37
49
websockifyUrl . protocol = window . location . protocol === "https:" ? "wss" : "ws" ;
38
50
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 ;
45
52
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
+ ) ;
49
60
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 ) ;
52
64
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 ;
55
67
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 ) ;
61
76
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
+ ) ;
65
89
}
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