From acc214ac745a25c84564249967feb19c1e7282d3 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Sun, 15 Oct 2023 20:13:19 +0300 Subject: [PATCH] Default to the same host when determing WebSocket URL We still need to be able to override it in order to use the Sveltekit dev server --- webif/src/lib/index.ts | 1 - webif/src/lib/websocket.ts | 9 +++++++++ webif/src/routes/+page.svelte | 8 ++------ 3 files changed, 11 insertions(+), 7 deletions(-) delete mode 100644 webif/src/lib/index.ts create mode 100644 webif/src/lib/websocket.ts diff --git a/webif/src/lib/index.ts b/webif/src/lib/index.ts deleted file mode 100644 index 856f2b6..0000000 --- a/webif/src/lib/index.ts +++ /dev/null @@ -1 +0,0 @@ -// place files you want to import through the `$lib` alias in this folder. diff --git a/webif/src/lib/websocket.ts b/webif/src/lib/websocket.ts new file mode 100644 index 0000000..493a429 --- /dev/null +++ b/webif/src/lib/websocket.ts @@ -0,0 +1,9 @@ +export const determineWebSocketUrl = () => { + const urlParams = new URLSearchParams(window.location.search) + + // Use whatever is specified by the "ws" query parameter, default + // to the current host of nothing is specified. This allows us to + // manually define the WebSocket host when running the Svelte + // development server. + return urlParams.get('ws') || `ws://${window.location.host}` +} diff --git a/webif/src/routes/+page.svelte b/webif/src/routes/+page.svelte index e3fb110..08df097 100644 --- a/webif/src/routes/+page.svelte +++ b/webif/src/routes/+page.svelte @@ -1,5 +1,7 @@