diff --git a/resources/js/webserver/connector.js b/resources/js/webserver/connector.js index 16085c3..6779756 100644 --- a/resources/js/webserver/connector.js +++ b/resources/js/webserver/connector.js @@ -1,18 +1,81 @@ +// function fromWindow() { +// const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:' +// return { +// host: (typeof window !== 'undefined' && window.location.hostname) || '127.0.0.1', +// port: isHttps ? 443 : 80, +// scheme: isHttps ? 'https' : 'http', +// path: '/ws', +// key: import.meta?.env?.VITE_REVERB_APP_KEY || '' +// } +// } +// const meta = readMetaConfig() +// const fallback = readViteEnv() +// const base = meta ?? fallback ?? fromWindow() +// +// // harte Korrektur: NIEMALS 8080 nach außen geben +// if (Number(base.port) === 8080 && (base.scheme || 'https').startsWith('http')) { +// base.port = (base.scheme === 'https') ? 443 : 80 +// } +// Liest Konfig aus +function readMetaConfig() { + const el = (typeof document !== 'undefined') + ? document.querySelector('meta[name="reverb"]') + : null; + if (!el) return null; + const { host, port, scheme, path, key } = el.dataset; + return { + host: host || null, + port: port ? Number(port) : null, + scheme: scheme || null, + path: path || null, + key: key || null, + }; +} + +// Fallback für DEV/HMR (Vite-Umgebungsvariablen) +function readViteEnv() { + const env = (typeof import.meta !== 'undefined' && import.meta.env) ? import.meta.env : {}; + return { + host: env.VITE_REVERB_HOST || env.APP_HOST || '127.0.0.1', + port: env.VITE_REVERB_PORT ? Number(env.VITE_REVERB_PORT) : 443, + scheme: (env.VITE_REVERB_SCHEME || 'https').toLowerCase(), + path: env.VITE_REVERB_PATH || '/ws', + key: env.VITE_REVERB_APP_KEY || '', + }; +} + function fromWindow() { - const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:' + const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:'; return { host: (typeof window !== 'undefined' && window.location.hostname) || '127.0.0.1', port: isHttps ? 443 : 80, scheme: isHttps ? 'https' : 'http', path: '/ws', - key: import.meta?.env?.VITE_REVERB_APP_KEY || '' - } + key: '', + }; } -const meta = readMetaConfig() -const fallback = readViteEnv() -const base = meta ?? fallback ?? fromWindow() -// harte Korrektur: NIEMALS 8080 nach außen geben -if (Number(base.port) === 8080 && (base.scheme || 'https').startsWith('http')) { - base.port = (base.scheme === 'https') ? 443 : 80 +function normalize(cfg) { + const c = { ...cfg }; + if (!c.path || typeof c.path !== 'string') c.path = '/ws'; + if (!c.path.startsWith('/')) c.path = '/' + c.path; + c.wsScheme = (c.scheme === 'http' ? 'ws' : 'wss'); + return c; } + +const meta = readMetaConfig(); +const fallback = readViteEnv(); +const base = meta ?? fallback ?? fromWindow(); + +// Niemals Port 8080 an den Browser geben – nach außen 80/443 nutzen +if (Number(base.port) === 8080 && (base.scheme || 'https').startsWith('http')) { + base.port = (base.scheme === 'https') ? 443 : 80; +} + +export const wsConfig = normalize({ + host: base.host || '127.0.0.1', + port: base.port || 443, + scheme: base.scheme || 'https', + path: base.path || '/ws', + key: base.key || '', +});