fix: 修正即時聊天室 presence channel 間歇性失效

三個根本原因:
1. updateEchoToken() 呼叫 disconnect/connect 會中斷已訂閱的
   presence channel,改為只更新 auth header(不重連)。
   同時將 setAuth() 的呼叫順序改為先 updateEchoToken() 再
   startRealtime(),確保訂閱時使用最新 token。
2. BookingPresenceChannel::join() 未對 schedule null 做防守,
   載入後若 schedule 不存在會 500,導致 auth 靜默失敗。
3. WebSocket 重連後 presence channel 不會自動恢復,
   在 BookingChat 加入 reconnected 事件處理,重連後重訂閱。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 17:46:15 +08:00
parent 6286399a3b
commit 1ba4ca893a
5 changed files with 21 additions and 7 deletions
+7 -4
View File
@@ -27,10 +27,13 @@ const echo = new Echo({
// 登入後更新 auth header,讓 presence channel 授權帶正確 token
export function updateEchoToken() {
const token = getAuthToken()
echo.options.auth.headers.Authorization = `Bearer ${token}`
// 重新連線讓新 token 生效
echo.disconnect()
echo.connect()
const bearer = `Bearer ${token}`
echo.options.auth.headers.Authorization = bearer
// 同步更新 Pusher 內部 config(避免 Pusher.js 持有舊的 header copy
if (echo.connector?.pusher?.config?.auth?.headers) {
echo.connector.pusher.config.auth.headers.Authorization = bearer
}
// 不 disconnect/connect:避免中斷 BookingChat 已訂閱的 presence channel
}
export default echo