66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
import axios from 'axios'
|
|
import Echo from 'laravel-echo'
|
|
import Pusher from 'pusher-js'
|
|
|
|
window.axios = axios
|
|
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
|
|
|
|
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content')
|
|
if (csrfToken) {
|
|
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = csrfToken
|
|
}
|
|
|
|
window.Pusher = Pusher
|
|
|
|
let echoInstance = null
|
|
|
|
function isLoopbackHost(host) {
|
|
return ['127.0.0.1', '0.0.0.0', 'localhost', '::1'].includes(String(host || '').toLowerCase())
|
|
}
|
|
|
|
export function getEcho() {
|
|
if (echoInstance !== null) {
|
|
return echoInstance || null
|
|
}
|
|
|
|
const key = import.meta.env.VITE_REVERB_APP_KEY
|
|
if (!key) {
|
|
echoInstance = false
|
|
return null
|
|
}
|
|
|
|
const scheme = import.meta.env.VITE_REVERB_SCHEME || window.location.protocol.replace(':', '') || 'https'
|
|
const forceTLS = scheme === 'https'
|
|
const configuredHost = import.meta.env.VITE_REVERB_HOST || window.location.hostname
|
|
const publicHostname = window.location.hostname
|
|
const useWindowHost = !isLoopbackHost(publicHostname) && isLoopbackHost(configuredHost)
|
|
const resolvedHost = useWindowHost ? publicHostname : configuredHost
|
|
const resolvedPort = useWindowHost
|
|
? Number(forceTLS ? 443 : 80)
|
|
: Number(import.meta.env.VITE_REVERB_PORT || (forceTLS ? 443 : 80))
|
|
const resolvedSecurePort = useWindowHost
|
|
? Number(forceTLS ? 443 : 80)
|
|
: Number(import.meta.env.VITE_REVERB_PORT || 443)
|
|
|
|
echoInstance = new Echo({
|
|
broadcaster: 'reverb',
|
|
key,
|
|
wsHost: resolvedHost,
|
|
wsPort: resolvedPort,
|
|
wssPort: resolvedSecurePort,
|
|
forceTLS,
|
|
enabledTransports: ['ws', 'wss'],
|
|
authEndpoint: '/broadcasting/auth',
|
|
auth: {
|
|
headers: {
|
|
'X-CSRF-TOKEN': csrfToken || '',
|
|
Accept: 'application/json',
|
|
},
|
|
},
|
|
})
|
|
|
|
window.Echo = echoInstance
|
|
|
|
return echoInstance
|
|
}
|