Skip to content

Commit

Permalink
Allow relative WebSocket URLs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CendioOssman committed Aug 8, 2024
1 parent 074fa1a commit 96c76f7
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,7 @@ const UI = {
UI.initSetting('logging', 'warn');
UI.updateLogging();

// if port == 80 (or 443) then it won't be present and should be
// set manually
let port = window.location.port;
if (!port) {
if (window.location.protocol.substring(0, 5) == 'https') {
port = 443;
} else if (window.location.protocol.substring(0, 4) == 'http') {
port = 80;
}
}

/* Populate the controls if defaults are provided in the URL */
UI.initSetting('host', window.location.hostname);
UI.initSetting('port', port);
UI.initSetting('encrypt', (window.location.protocol === "https:"));
UI.initSetting('view_clip', false);
UI.initSetting('resize', 'off');
Expand Down Expand Up @@ -1021,25 +1008,27 @@ const UI = {

UI.hideStatus();

if (!host) {
Log.Error("Can't connect when host is: " + host);
UI.showStatus(_("Must set host"), 'error');
return;
}

UI.closeConnectPanel();

UI.updateVisualState('connecting');

let url;

url = new URL("https://" + host);
if (host) {
url = new URL("https://" + host);

url.protocol = UI.getSetting('encrypt') ? 'wss:' : 'ws:';
if (port) {
url.port = port;
url.protocol = UI.getSetting('encrypt') ? 'wss:' : 'ws:';
if (port) {
url.port = port;
}
url.pathname = '/' + path;
} else {
// Current (May 2024) browsers support relative WebSocket
// URLs natively, but we need to support older browsers for
// some time.
url = new URL(path, location.href);
url.protocol = (window.location.protocol === "https:") ? 'wss:' : 'ws:';
}
url.pathname = '/' + path;

try {
UI.rfb = new RFB(document.getElementById('noVNC_container'),
Expand Down

2 comments on commit 96c76f7

@XiaoXianNv-boot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PORT SETUP No response !!!!!!

rul: vnc?port=8880&path=websockify?token=qemu_PVE&autoconnect=1&resize=browser

@CendioOssman
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using a different port, you must now also explicitly specify the host. This is more in line with how URLs work, where you cannot specify just a different port.

Please sign in to comment.