61 lines
1.4 KiB
PHP
61 lines
1.4 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\ShouldBroadcast;
|
||
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
|
||
|
|
class UserJoined implements ShouldBroadcast
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new event instance.
|
||
|
|
*
|
||
|
|
* @param int $roomId 房间ID
|
||
|
|
* @param string $username 进入的用户昵称
|
||
|
|
* @param array $userInfo 用户的属性(如性别,等级等)
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public readonly int $roomId,
|
||
|
|
public readonly string $username,
|
||
|
|
public readonly array $userInfo,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the channels the event should broadcast on.
|
||
|
|
*
|
||
|
|
* @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,
|
||
|
|
'info' => $this->userInfo,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|