From 686a453fc507875c3726f30ca587fd0602065a57 Mon Sep 17 00:00:00 2001 From: Alain Nussbaumer Date: Sat, 18 Jan 2025 16:54:02 +0100 Subject: [PATCH] [web] Simplify WebSocket creation and remove unused translation --- web-src/src/App.vue | 57 ++++++---------------------- web-src/src/i18n/de.json | 1 - web-src/src/i18n/en.json | 1 - web-src/src/i18n/fr.json | 1 - web-src/src/i18n/zh-CN.json | 1 - web-src/src/i18n/zh-TW.json | 1 - web-src/src/pages/PageNowPlaying.vue | 2 +- 7 files changed, 13 insertions(+), 51 deletions(-) diff --git a/web-src/src/App.vue b/web-src/src/App.vue index a3481040..2f86aee6 100644 --- a/web-src/src/App.vue +++ b/web-src/src/App.vue @@ -144,7 +144,7 @@ export default { this.configurationStore.$state = data this.uiStore.hide_singles = data.hide_singles document.title = data.library_name - this.open_ws() + this.open_websocket() this.$Progress.finish() }) .catch(() => { @@ -155,10 +155,8 @@ export default { }) }) }, - open_ws() { - const wsPort = this.configurationStore.websocket_port - const socket = wsPort <= 0 ? this.create_websocket() : this.create_websocket_with_port(wsPort) - + open_websocket() { + const socket = this.create_websocket() const vm = this socket.onopen = () => { vm.reconnect_attempts = 0 @@ -258,47 +256,18 @@ export default { } }, create_websocket() { - // Create websocket connection on the same http connection - const wsHost = window.location.hostname - const wsPath = '/ws' - const wsPort = window.location.port - - let wsProtocol = 'ws://' - if (window.location.protocol === 'https:') { - wsProtocol = 'wss://' - } - - const wsUrl = `${wsProtocol}${wsHost}:${wsPort}${wsPath}` - - const socket = new ReconnectingWebSocket(wsUrl, 'notify', { + const protocol = window.location.protocol.replace('http', 'ws') + const hostname = + (import.meta.env.DEV && + URL.parse(import.meta.env.VITE_OWNTONE_URL)?.hostname) || + window.location.hostname + const suffix = + this.configurationStore.websocket_port || `${window.location.port}/ws` + const url = `${protocol}${hostname}:${suffix}` + return new ReconnectingWebSocket(url, 'notify', { maxReconnectInterval: 2000, reconnectInterval: 1000 }) - return socket - }, - create_websocket_with_port(port) { - // Create websocket connection on a separate http port - let protocol = 'ws://' - if (window.location.protocol === 'https:') { - protocol = 'wss://' - } - - let wsUrl = `${protocol}${window.location.hostname}:${port}` - - if (import.meta.env.DEV && import.meta.env.VITE_OWNTONE_URL) { - /* - * If we are running in development mode, construct the websocket - * url from the host of the environment variable VITE_OWNTONE_URL - */ - const url = new URL(import.meta.env.VITE_OWNTONE_URL) - wsUrl = `${protocol}${url.hostname}:${port}` - } - - const socket = new ReconnectingWebSocket(wsUrl, 'notify', { - maxReconnectInterval: 2000, - reconnectInterval: 1000 - }) - return socket }, update_is_clipped() { if (this.show_burger_menu || this.show_player_menu) { @@ -377,5 +346,3 @@ export default { template: '' } - - diff --git a/web-src/src/i18n/de.json b/web-src/src/i18n/de.json index c01c9564..256cee17 100644 --- a/web-src/src/i18n/de.json +++ b/web-src/src/i18n/de.json @@ -578,7 +578,6 @@ }, "server": { "connection-failed": "Fehler bei Verbindung zum OwnTone-Server", - "missing-port": "Fehlender Websocket-Port", "request-failed": "Anfrage gescheitert (Status: {status} {cause} {url})", "queue-saved": "Warteschlange zu Playlist {name} gesichert", "appended-tracks": "{count} Track an die Abspielliste angehängt|{count} Tracks an die Abspielliste angehängt", diff --git a/web-src/src/i18n/en.json b/web-src/src/i18n/en.json index e68914ba..77c8ac47 100644 --- a/web-src/src/i18n/en.json +++ b/web-src/src/i18n/en.json @@ -577,7 +577,6 @@ }, "server": { "connection-failed": "Failed to connect to OwnTone server", - "missing-port": "Missing websocket port", "request-failed": "Request failed (status: {status} {cause} {url})", "queue-saved": "Queue saved to playlist {name}", "appended-tracks": "{count} track appended to the queue|{count} tracks appended to the queue", diff --git a/web-src/src/i18n/fr.json b/web-src/src/i18n/fr.json index 01db1c42..a36aadd1 100644 --- a/web-src/src/i18n/fr.json +++ b/web-src/src/i18n/fr.json @@ -577,7 +577,6 @@ }, "server": { "connection-failed": "Échec de connexion au serveur", - "missing-port": "Port websocket manquant", "request-failed": "La requête a échoué (status: {status} {cause} {url})", "queue-saved": "La file d’attente enregistrée dans la liste de lecture {name}", "appended-tracks": "{count} piste ajoutée à la file d’attente|{count} pistes ajoutées à la file d’attente", diff --git a/web-src/src/i18n/zh-CN.json b/web-src/src/i18n/zh-CN.json index 25d5925d..fb8f0fae 100644 --- a/web-src/src/i18n/zh-CN.json +++ b/web-src/src/i18n/zh-CN.json @@ -577,7 +577,6 @@ }, "server": { "connection-failed": "无法连接到 OwnTone 服务器", - "missing-port": "缺少 websocket 端口", "request-failed": "请求失败 (状态:{status} {cause} {url})", "queue-saved": "清单以添加到播放列表 {name}", "appended-tracks": "已附加到队列的 {count} 只曲目|已附加到队列的 {count} 只曲目", diff --git a/web-src/src/i18n/zh-TW.json b/web-src/src/i18n/zh-TW.json index b8af040d..89be5902 100644 --- a/web-src/src/i18n/zh-TW.json +++ b/web-src/src/i18n/zh-TW.json @@ -577,7 +577,6 @@ }, "server": { "connection-failed": "無法連接到 OwnTone 伺服器", - "missing-port": "缺少 websocket 端口", "request-failed": "請求失敗 (狀態:{status} {cause} {url})", "queue-saved": "清單以新增到播放列表 {name}", "appended-tracks": "已附加到隊列的 {count} 首曲目|已附加到隊列的 {count} 首曲目", diff --git a/web-src/src/pages/PageNowPlaying.vue b/web-src/src/pages/PageNowPlaying.vue index b03293d4..3186a718 100644 --- a/web-src/src/pages/PageNowPlaying.vue +++ b/web-src/src/pages/PageNowPlaying.vue @@ -153,7 +153,7 @@ export default { if (newState === 'play') { this.interval_id = window.setInterval(this.tick, INTERVAL) } - }, + } }, created() {