💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### laravel-websockets 替代pusher 发送频道消息 > https://beyondco.de/docs/laravel-websockets/basic-usage/pusher 首先按照文档安装好laravel-websocket,然后设置好替换puhser的配置即可。 其中自带的接口是 `broadcasting/auth` > app\Providers\BroadcastServiceProvider.php ~~~ use Illuminate\Support\Facades\Broadcast; /** * Bootstrap any application services. * * @return void */ public function boot() { // 自己定义路由中间件 Broadcast::routes(['prefix'=>'api','middleware' => 'auth:api']); require base_path('routes/channels.php'); } ~~~ 这样配置后默认的地址便是 `域名+api/broadcasting/auth` 其中auth使用的是jwt扩展 `composer require tymon/jwt-auth` 参数 :header头需要加上 token | 参数名 | 值| | --- | --- | | channel_name | public_channle | | socket_id| 98834966.500888313| 加上token一个三个参数,访问即可得到一个auth:签名秘钥 > {"auth":签名秘钥} 有了这个auth便可去订阅那些需要登录的频道。前端使用laravel-echo 其中发送消息需要创建 event ~~~ namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; use App\Models\User\User; class PublicMessages implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels; public $message; // 消息 public function __construct( $message ) { $this->message = $message; } /** * 队列名 * @return string */ public function broadcastQueue() { return 'message'; } /** * The event's broadcast name. * 一般都是配置的redis ,不是sync sync便是立即发送。redis使用队列,在这里可以指定队列名称 * 需要开启消费队列,这样才能收到消息 * @return string */ public function broadcastAs() { return 'message'; } public function broadcastOn() { return new Channel('public'); } /** * 用于扩展字段的 * Get the data to broadcast. * * @return array */ public function broadcastWith() { return [ 'message' => $this->message, ]; } } ~~~ #### 路由设置 ~~~ / 公共频道 Broadcast::channel('public', function ($user) { // 公共频道直接返回true即可 return true; }); ~~~ ~~~ // 用户私有频道 Broadcast::channel('private.{userId}', function ($user, $userId) { 因为加了登录验证的jwt,的中间件,他会去解析出用户信息,赋给$user 这里第一个参数都是 $user实例对象,后面参数就是对应自己设置的{userId},在此对比是否一致,就能判断是否是本人。详情就看laravel广播那一节手册 return (int)$user->id === (int)$userId; }); ~~~ #### 发送消息 ~~~ broadcast(new PublicMessages('this is a messgae')); ~~~ 上面代码是关于公共频道的,私有频道自己加上用户参数即可 #### 广播配置文件配置 config/broadcasting.php 如下 ~~~ 'connections' => [ 'pusher' => [ 'driver' => 'pusher', 'key' => env('PUSHER_APP_KEY'), 'secret' => env('PUSHER_APP_SECRET'), 'app_id' => env('PUSHER_APP_ID'), 'options' => [ 'cluster' => env('PUSHER_APP_CLUSTER'), 'encrypted' => true, 'host' => '127.0.0.1', 'port' => 6001, 'scheme' => 'http' ], ] ], ~~~ #### config/websockets.php文件配置如下 ~~~ 'apps' => [ [ 'id' => env('PUSHER_APP_ID'), 'name' => env('APP_NAME'), 'key' => env('PUSHER_APP_KEY'), 'secret' => env('PUSHER_APP_SECRET'), 'enable_client_messages' => false, 'enable_statistics' => true, ] ], ~~~ #### .env中配置如下 > BROADCAST_DRIVER=pusher PUSHER_APP_ID=配置id PUSHER_APP_KEY=配置key PUSHER_APP_SECRET=配置秘钥 #其中的id、key、secret 根据项目需求随意配置即可,因为是用的websockets替代了pusher,如果是用的pusher提供原服务需要去pusher官网申请其中的id、key、secret 配置即可