From 886c8e31de85a9590a581f97358472a988b3d2b7 Mon Sep 17 00:00:00 2001 From: Mohamed Ali Elsayed Date: Tue, 7 Jan 2025 16:16:42 +0300 Subject: [PATCH] fix: handle WebSocket URL construction for applications hosted in subfolders or behind proxies - Updated WebSocket URL logic to dynamically handle subfolder paths. - Ensured compatibility with applications hosted behind proxies by correctly constructing the WebSocket URL based on the current document location. - Added support for both `ws://` and `wss://` protocols based on the application's hosting environment. --- src/views/components/AppLogin.js | 2 +- src/views/components/AppLogout.js | 2 +- src/views/index.html | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/views/components/AppLogin.js b/src/views/components/AppLogin.js index 20cd062..81f0c3c 100644 --- a/src/views/components/AppLogin.js +++ b/src/views/components/AppLogin.js @@ -26,7 +26,7 @@ export default { }, async submitApi() { try { - let response = await window.http.get(`/app/login`) + let response = await window.http.get(`./app/login`) let results = response.data.results; this.login_link = results.qr_link; this.login_duration_sec = results.qr_duration; diff --git a/src/views/components/AppLogout.js b/src/views/components/AppLogout.js index f0ed901..bc1d66d 100644 --- a/src/views/components/AppLogout.js +++ b/src/views/components/AppLogout.js @@ -15,7 +15,7 @@ export default { async submitApi() { try { - await http.get(`/app/logout`) + await http.get(`./app/logout`) } catch (error) { if (error.response) { throw Error(error.response.data.message) diff --git a/src/views/index.html b/src/views/index.html index 564749d..226ccd8 100644 --- a/src/views/index.html +++ b/src/views/index.html @@ -204,7 +204,14 @@ mounted() { if (window["WebSocket"]) { let wsType = location.protocol !== 'https:' ? 'ws://' : 'wss://'; - this.app_ws = new WebSocket(wsType + document.location.host + "/ws"); + let path = document.location.pathname; // Get the full path including the subfolder + // Remove trailing slash if any + if (path.endsWith('/')) { + path = path.slice(0, -1); + } + // Construct the WebSocket URL with the full path + this.app_ws = new WebSocket(wsType + document.location.host + path + "/ws"); + this.app_ws.onopen = (evt) => { this.app_ws.send(JSON.stringify({