58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 文件功能:聊天室用户状态变更广播事件
|
||
|
|
* 负责在用户设置或清除当日状态后,实时同步当前房间在线名单展示。
|
||
|
|
*/
|
||
|
|
|
||
|
|
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 UserStatusUpdated implements ShouldBroadcastNow
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 构造聊天室用户状态变更广播事件。
|
||
|
|
*
|
||
|
|
* @param int $roomId 房间 ID
|
||
|
|
* @param string $username 状态变更用户昵称
|
||
|
|
* @param array<string, mixed> $user 最新在线名单载荷
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public readonly int $roomId,
|
||
|
|
public readonly string $username,
|
||
|
|
public readonly array $user,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取广播频道。
|
||
|
|
*
|
||
|
|
* @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 [
|
||
|
|
'username' => $this->username,
|
||
|
|
'user' => $this->user,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|