495efdf9e0
- 输入框输入 / 弹出命令菜单,当前支持 /拍一拍 - 选择对象后输入 /拍一拍 发送拍一拍通知 - 所有在线用户屏幕抖动 + 正常聊天样式显示消息 - 命令注册表可扩展,后续新增命令只需 push 到数组
69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文件功能:用户"拍一拍"广播事件
|
|
*
|
|
* 用户输入 /拍一拍 用户名 后触发,通过 WebSocket 广播给房间内所有用户,
|
|
* 前端显示 "XXX拍了拍XXX" 消息并触发屏幕抖动动画。
|
|
*
|
|
* @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 UserPat implements ShouldBroadcastNow
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @param int $roomId 房间 ID
|
|
* @param string $fromUser 拍人的用户
|
|
* @param string $targetUser 被拍的用户
|
|
* @param string $displayText 前端展示文本,如 "流星 拍了拍 张三"
|
|
*/
|
|
public function __construct(
|
|
public readonly int $roomId,
|
|
public readonly string $fromUser,
|
|
public readonly string $targetUser,
|
|
public readonly string $displayText,
|
|
public readonly ?string $fromUserHeadface = null,
|
|
) {}
|
|
|
|
/**
|
|
* 广播频道:向房间内所有在线用户推送
|
|
*
|
|
* @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 [
|
|
'from_user' => $this->fromUser,
|
|
'target_user' => $this->targetUser,
|
|
'display_text' => $this->displayText,
|
|
'from_user_headface' => $this->fromUserHeadface,
|
|
];
|
|
}
|
|
}
|