[web] Simplify WebSocket creation and remove unused translation

This commit is contained in:
Alain Nussbaumer 2025-01-18 16:54:02 +01:00
parent 9420c52bf7
commit 386ad61bc8
6 changed files with 12 additions and 48 deletions

View File

@ -141,7 +141,7 @@ export default {
this.configurationStore.$state = data this.configurationStore.$state = data
this.uiStore.hide_singles = data.hide_singles this.uiStore.hide_singles = data.hide_singles
document.title = data.library_name document.title = data.library_name
this.open_ws() this.open_websocket()
this.$Progress.finish() this.$Progress.finish()
}) })
.catch(() => { .catch(() => {
@ -152,10 +152,8 @@ export default {
}) })
}) })
}, },
open_ws() { open_websocket() {
const wsPort = this.configurationStore.websocket_port const socket = this.create_websocket()
const socket = wsPort <= 0 ? this.create_websocket() : this.create_websocket_with_port(wsPort)
const vm = this const vm = this
socket.onopen = () => { socket.onopen = () => {
vm.reconnect_attempts = 0 vm.reconnect_attempts = 0
@ -255,47 +253,18 @@ export default {
} }
}, },
create_websocket() { create_websocket() {
// Create websocket connection on the same http connection const protocol = window.location.protocol.replace('http', 'ws')
const wsHost = window.location.hostname const hostname =
const wsPath = '/ws' (import.meta.env.DEV &&
const wsPort = window.location.port URL.parse(import.meta.env.VITE_OWNTONE_URL)?.hostname) ||
window.location.hostname
let wsProtocol = 'ws://' const suffix =
if (window.location.protocol === 'https:') { this.configurationStore.websocket_port || `${window.location.port}/ws`
wsProtocol = 'wss://' const url = `${protocol}${hostname}:${suffix}`
} return new ReconnectingWebSocket(url, 'notify', {
const wsUrl = `${wsProtocol}${wsHost}:${wsPort}${wsPath}`
const socket = new ReconnectingWebSocket(wsUrl, 'notify', {
maxReconnectInterval: 2000, maxReconnectInterval: 2000,
reconnectInterval: 1000 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() { update_is_clipped() {
if (this.show_burger_menu || this.show_player_menu) { if (this.show_burger_menu || this.show_player_menu) {

View File

@ -574,7 +574,6 @@
}, },
"server": { "server": {
"connection-failed": "Fehler bei Verbindung zum OwnTone-Server", "connection-failed": "Fehler bei Verbindung zum OwnTone-Server",
"missing-port": "Fehlender Websocket-Port",
"request-failed": "Anfrage gescheitert (Status: {status} {cause} {url})", "request-failed": "Anfrage gescheitert (Status: {status} {cause} {url})",
"queue-saved": "Warteschlange zu Playlist {name} gesichert", "queue-saved": "Warteschlange zu Playlist {name} gesichert",
"appended-tracks": "{count} Track an die Abspielliste angehängt|{count} Tracks an die Abspielliste angehängt", "appended-tracks": "{count} Track an die Abspielliste angehängt|{count} Tracks an die Abspielliste angehängt",

View File

@ -573,7 +573,6 @@
}, },
"server": { "server": {
"connection-failed": "Failed to connect to OwnTone server", "connection-failed": "Failed to connect to OwnTone server",
"missing-port": "Missing websocket port",
"request-failed": "Request failed (status: {status} {cause} {url})", "request-failed": "Request failed (status: {status} {cause} {url})",
"queue-saved": "Queue saved to playlist {name}", "queue-saved": "Queue saved to playlist {name}",
"appended-tracks": "{count} track appended to the queue|{count} tracks appended to the queue", "appended-tracks": "{count} track appended to the queue|{count} tracks appended to the queue",

View File

@ -573,7 +573,6 @@
}, },
"server": { "server": {
"connection-failed": "Échec de connexion au serveur", "connection-failed": "Échec de connexion au serveur",
"missing-port": "Port websocket manquant",
"request-failed": "La requête a échoué (status: {status} {cause} {url})", "request-failed": "La requête a échoué (status: {status} {cause} {url})",
"queue-saved": "La file dattente enregistrée dans la liste de lecture {name}", "queue-saved": "La file dattente enregistrée dans la liste de lecture {name}",
"appended-tracks": "{count} piste ajoutée à la file dattente|{count} pistes ajoutées à la file dattente", "appended-tracks": "{count} piste ajoutée à la file dattente|{count} pistes ajoutées à la file dattente",

View File

@ -573,7 +573,6 @@
}, },
"server": { "server": {
"connection-failed": "无法连接到 OwnTone 服务器", "connection-failed": "无法连接到 OwnTone 服务器",
"missing-port": "缺少 websocket 端口",
"request-failed": "请求失败 (状态:{status} {cause} {url})", "request-failed": "请求失败 (状态:{status} {cause} {url})",
"queue-saved": "清单以添加到播放列表 {name}", "queue-saved": "清单以添加到播放列表 {name}",
"appended-tracks": "已附加到队列的 {count} 只曲目|已附加到队列的 {count} 只曲目", "appended-tracks": "已附加到队列的 {count} 只曲目|已附加到队列的 {count} 只曲目",

View File

@ -573,7 +573,6 @@
}, },
"server": { "server": {
"connection-failed": "無法連接到 OwnTone 伺服器", "connection-failed": "無法連接到 OwnTone 伺服器",
"missing-port": "缺少 websocket 端口",
"request-failed": "請求失敗 (狀態:{status} {cause} {url})", "request-failed": "請求失敗 (狀態:{status} {cause} {url})",
"queue-saved": "清單以新增到播放列表 {name}", "queue-saved": "清單以新增到播放列表 {name}",
"appended-tracks": "已附加到隊列的 {count} 首曲目|已附加到隊列的 {count} 首曲目", "appended-tracks": "已附加到隊列的 {count} 首曲目|已附加到隊列的 {count} 首曲目",