Files
CFDivePlatform/frontend/src/plugins/echo.js
T
a620906209 5ba598c78f fix: 修正聊天室雙向即時同步失效問題
- echo.js:還原 disconnect()/connect(),確保 Reverb 重連後重新授權 presence channel
- BookingChat.vue:移除 handleNotificationFallback(會覆蓋樂觀更新訊息導致重複)
- BookingChat.vue:改用 state_change 事件偵測重連('reconnected' 並非合法 Pusher.js 事件)
- BookingChat.vue:移除未使用的 useAuthStore / useCoachAuthStore / currentUserId 依賴

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-28 18:29:53 +08:00

40 lines
1.2 KiB
JavaScript

import Echo from 'laravel-echo'
import Pusher from 'pusher-js'
window.Pusher = Pusher
function getAuthToken() {
return localStorage.getItem('coach_token') || localStorage.getItem('token') || null
}
const echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 443,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
authEndpoint: `${import.meta.env.VITE_API_URL}/api/broadcasting/auth`,
auth: {
headers: {
Authorization: `Bearer ${getAuthToken()}`,
Accept: 'application/json',
},
},
})
// 登入後更新 auth header 並強制重連,確保 Reverb 用新 token 授權 channel
export function updateEchoToken() {
const token = getAuthToken()
const bearer = `Bearer ${token}`
echo.options.auth.headers.Authorization = bearer
if (echo.connector?.pusher?.config?.auth?.headers) {
echo.connector.pusher.config.auth.headers.Authorization = bearer
}
echo.disconnect()
echo.connect()
}
export default echo