- ScreenCleared 改用 ShouldBroadcastNow 绕过队列,避免 Horizon 未重启问题 - Echo 监听移入 DOMContentLoaded + 500ms 重试,确保 window.Echo 就绪后再注册 - 添加 console.log 便于调试
60 lines
1.3 KiB
PHP
60 lines
1.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文件功能:管理员全员清屏广播事件
|
|
*
|
|
* 管理员触发清屏后,广播给房间内所有用户,前端监听后清除聊天记录(悄悄话除外)。
|
|
*
|
|
* @author ChatRoom Laravel
|
|
*
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
namespace App\Events;
|
|
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Broadcasting\PresenceChannel;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class ScreenCleared implements ShouldBroadcastNow
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @param int $roomId 房间ID
|
|
* @param string $operator 执行清屏的管理员用户名
|
|
*/
|
|
public function __construct(
|
|
public readonly int $roomId,
|
|
public readonly string $operator,
|
|
) {}
|
|
|
|
/**
|
|
* 广播频道
|
|
*
|
|
* @return array<int, \Illuminate\Broadcasting\Channel>
|
|
*/
|
|
public function broadcastOn(): array
|
|
{
|
|
return [
|
|
new PresenceChannel('room.'.$this->roomId),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 广播数据
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function broadcastWith(): array
|
|
{
|
|
return [
|
|
'operator' => $this->operator,
|
|
];
|
|
}
|
|
}
|