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 MessageSent implements ShouldBroadcastNow
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
/**
|
|
* Create a new event instance.
|
|
*
|
|
* @param int $roomId 房间ID
|
|
* @param array $message 发送的消息数据
|
|
*/
|
|
public function __construct(
|
|
public readonly int $roomId,
|
|
public readonly array $message,
|
|
) {}
|
|
|
|
/**
|
|
* Get the channels the event should broadcast on.
|
|
*
|
|
* 聊天消息广播至包含在线状态管理的 PresenceChannel。
|
|
*
|
|
* @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 [
|
|
'message' => $this->message,
|
|
];
|
|
}
|
|
}
|