diff --git a/app/Events/ScreenCleared.php b/app/Events/ScreenCleared.php index d8451ae..517446f 100644 --- a/app/Events/ScreenCleared.php +++ b/app/Events/ScreenCleared.php @@ -14,11 +14,11 @@ namespace App\Events; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; -use Illuminate\Contracts\Broadcasting\ShouldBroadcast; +use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; -class ScreenCleared implements ShouldBroadcast +class ScreenCleared implements ShouldBroadcastNow { use Dispatchable, InteractsWithSockets, SerializesModels; diff --git a/resources/views/chat/partials/scripts.blade.php b/resources/views/chat/partials/scripts.blade.php index dfe54dc..a2b96f0 100644 --- a/resources/views/chat/partials/scripts.blade.php +++ b/resources/views/chat/partials/scripts.blade.php @@ -514,10 +514,16 @@ document.getElementById('room-title-display').innerText = e.detail.title; }); - // ── 管理员全员清屏事件(直接用 Echo 监听,不依赖编译产物) ─────── - if (window.Echo) { + // ── 管理员全员清屏事件(等待 Echo 就绪后监听) ─────── + function setupScreenClearedListener() { + if (!window.Echo || !window.chatContext) { + // Echo 或 chatContext 还没就绪,延迟重试 + setTimeout(setupScreenClearedListener, 500); + return; + } window.Echo.join(`room.${window.chatContext.roomId}`) .listen('ScreenCleared', (e) => { + console.log('收到全员清屏事件:', e); const operator = e.operator; // 清除公聊窗口(say1)所有消息 @@ -550,7 +556,10 @@ say1.scrollTop = say1.scrollHeight; } }); + console.log('ScreenCleared 监听器已注册'); } + // DOMContentLoaded 后启动,确保 Vite 编译的 JS 已加载 + document.addEventListener('DOMContentLoaded', setupScreenClearedListener); // ── 发送消息(Enter 发送) ─────────────────────── document.getElementById('content').addEventListener('keydown', function(e) {