一、注册pusher账户并创建一个应用
打开网址: https://pusher.com注册获取应用秘钥
二、安装配置Pusher
1.安装
composer require pusher/pusher-php-server
2.打开config/broadcasting.php 进行如下配置
'default' => env('BROADCAST_DRIVER', 'pusher'),
'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','ap3'),
'useTLS' => true,
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
]
3.配置ENV
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=xxxxxxx
PUSHER_APP_KEY=xxxxxxx
PUSHER_APP_SECRET=xxxxxxx
PUSHER_APP_CLUSTER=xxxxxxx
三、事件
1.创建Pusher
php artisan make:event PusherEvent
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class PusherEvent implements ShouldBroadcast
{
use SerializesModels;
public $info;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($info)
{
$this->info = $info;
}
/**
* 指定广播频道
* @return string[]
*/
public function broadcastOn()
{
return ['my-channel'];
}
/**
* 指定广播事件
* @return string
*/
public function broadcastAs()
{
return 'my-event';
}
public function broadcastWith()
{
return ['info' => $this->info];
}
}
四、触发上面的测试事件
1.控制器中触发event(new PusherEvent('测试'));
<?php
namespace App\Http\Controllers;
use App\Events\PusherEvent;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
return view('index');
}
public function test(Request $request)
{
try {
event(new PusherEvent('测试'));
dd('success');
} catch (\Exception $exception) {
dd($exception->getMessage());
}
}
}
2.也可以使用artisan 命令来触发
php artisan make:command PusherEventCommand
找到 app/Console/Commands/PusherEventCommand.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PusherEventCommand extends Command
{
protected $signature = 'pusher:test {message}';
protected $description = 'pusher test';
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
event(new \App\Events\PusherEvent($this->argument('message')));
}
}
找到 app/Console/Kernel.php 将创建的命令添加到 $commands中
protected $commands = [
\App\Console\Commands\PusherEventCommand::class,
];
到这里就配置完了 ,可以使用artisan命令测试一下
php artisan pusher:test "这是一个命令测试"
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有