修复:全员清屏广播不生效

- ScreenCleared 改用 ShouldBroadcastNow 绕过队列,避免 Horizon 未重启问题
- Echo 监听移入 DOMContentLoaded + 500ms 重试,确保 window.Echo 就绪后再注册
- 添加 console.log 便于调试
This commit is contained in:
2026-02-26 23:43:09 +08:00
parent f92fae599e
commit 62b963fb9b
2 changed files with 13 additions and 4 deletions
+2 -2
View File
@@ -14,11 +14,11 @@ namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
class ScreenCleared implements ShouldBroadcast class ScreenCleared implements ShouldBroadcastNow
{ {
use Dispatchable, InteractsWithSockets, SerializesModels; use Dispatchable, InteractsWithSockets, SerializesModels;
@@ -514,10 +514,16 @@
document.getElementById('room-title-display').innerText = e.detail.title; document.getElementById('room-title-display').innerText = e.detail.title;
}); });
// ── 管理员全员清屏事件(直接用 Echo 监听,不依赖编译产物 ─────── // ── 管理员全员清屏事件(等待 Echo 就绪后监听) ───────
if (window.Echo) { function setupScreenClearedListener() {
if (!window.Echo || !window.chatContext) {
// Echo 或 chatContext 还没就绪,延迟重试
setTimeout(setupScreenClearedListener, 500);
return;
}
window.Echo.join(`room.${window.chatContext.roomId}`) window.Echo.join(`room.${window.chatContext.roomId}`)
.listen('ScreenCleared', (e) => { .listen('ScreenCleared', (e) => {
console.log('收到全员清屏事件:', e);
const operator = e.operator; const operator = e.operator;
// 清除公聊窗口(say1)所有消息 // 清除公聊窗口(say1)所有消息
@@ -550,7 +556,10 @@
say1.scrollTop = say1.scrollHeight; say1.scrollTop = say1.scrollHeight;
} }
}); });
console.log('ScreenCleared 监听器已注册');
} }
// DOMContentLoaded 后启动,确保 Vite 编译的 JS 已加载
document.addEventListener('DOMContentLoaded', setupScreenClearedListener);
// ── 发送消息(Enter 发送) ─────────────────────── // ── 发送消息(Enter 发送) ───────────────────────
document.getElementById('content').addEventListener('keydown', function(e) { document.getElementById('content').addEventListener('keydown', function(e) {