Skip to content

Commit 96c76f7

Browse files
committed
Allow relative WebSocket URLs
This can be very useful if you have multiple instances of noVNC, and you want to redirect them to different VNC servers. The new default settings will have the same behaviour as before for systems where noVNC is deployed in the root web folder.
1 parent 074fa1a commit 96c76f7

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

app/ui.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,7 @@ const UI = {
158158
UI.initSetting('logging', 'warn');
159159
UI.updateLogging();
160160

161-
// if port == 80 (or 443) then it won't be present and should be
162-
// set manually
163-
let port = window.location.port;
164-
if (!port) {
165-
if (window.location.protocol.substring(0, 5) == 'https') {
166-
port = 443;
167-
} else if (window.location.protocol.substring(0, 4) == 'http') {
168-
port = 80;
169-
}
170-
}
171-
172161
/* Populate the controls if defaults are provided in the URL */
173-
UI.initSetting('host', window.location.hostname);
174-
UI.initSetting('port', port);
175162
UI.initSetting('encrypt', (window.location.protocol === "https:"));
176163
UI.initSetting('view_clip', false);
177164
UI.initSetting('resize', 'off');
@@ -1021,25 +1008,27 @@ const UI = {
10211008

10221009
UI.hideStatus();
10231010

1024-
if (!host) {
1025-
Log.Error("Can't connect when host is: " + host);
1026-
UI.showStatus(_("Must set host"), 'error');
1027-
return;
1028-
}
1029-
10301011
UI.closeConnectPanel();
10311012

10321013
UI.updateVisualState('connecting');
10331014

10341015
let url;
10351016

1036-
url = new URL("https://" + host);
1017+
if (host) {
1018+
url = new URL("https://" + host);
10371019

1038-
url.protocol = UI.getSetting('encrypt') ? 'wss:' : 'ws:';
1039-
if (port) {
1040-
url.port = port;
1020+
url.protocol = UI.getSetting('encrypt') ? 'wss:' : 'ws:';
1021+
if (port) {
1022+
url.port = port;
1023+
}
1024+
url.pathname = '/' + path;
1025+
} else {
1026+
// Current (May 2024) browsers support relative WebSocket
1027+
// URLs natively, but we need to support older browsers for
1028+
// some time.
1029+
url = new URL(path, location.href);
1030+
url.protocol = (window.location.protocol === "https:") ? 'wss:' : 'ws:';
10411031
}
1042-
url.pathname = '/' + path;
10431032

10441033
try {
10451034
UI.rfb = new RFB(document.getElementById('noVNC_container'),

0 commit comments

Comments
 (0)