vinkla/推动器
Requires
- php: ^7.0
- graham-campbell/manager: ^3.0|^4.0
- illuminate/contracts: ^5.5|^6.0|^7.0
- illuminate/support: ^5.5|^6.0|^7.0
- pusher/pusher-php-server: ^3.3|^4.0
Requires (Dev)
- graham-campbell/analyzer: ^2.4
- graham-campbell/testbench: ^4.0|^5.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^6.5|^7.0|^8.0
README
Laravel 现在已内置对 Pusher Channels 的支持。[前往文档](https://laravel.net.cn/docs/master/broadcasting)。这是将 Channels 集成到 Laravel 项目的推荐方法。
目前,Pusher 将继续审查任何 PR 并解决此 SDK 中的安全漏洞,但不会进行任何重大改进。
Pusher Channels Laravel 库
Laravel 的 Pusher Channels 桥接器。以前为 vinkla/pusher
。
// Triggering events. $pusher->trigger('my-channel', 'my_event', 'hello world'); // Authenticating Private channels. $pusher->socket_auth('my-channel', 'socket_id'); // Want to use the facade? Pusher::get('/channels');
安装
使用 Composer,在项目的根目录中要求此包。
$ composer require pusher/pusher-http-laravel
将服务提供者添加到 config/app.php
中的 providers
数组。如果您使用的是 Laravel 5.5 或更高版本,则不需要这样做。
Pusher\Laravel\PusherServiceProvider::class
如果您想使用 外观,请将引用添加到 config/app.php
中的别名数组。
'Pusher' => Pusher\Laravel\Facades\Pusher::class
配置
Laravel Channels SDK 需要连接配置。要开始,您需要发布所有供应商资产
$ php artisan vendor:publish --provider="Pusher\Laravel\PusherServiceProvider"
这将创建一个您可以在其中修改配置的 config/pusher.php
文件。同时,请确保检查此包中原始配置文件在版本之间的更改。
默认连接名称
此选项 default
是您可能指定要使用以下哪个连接作为所有工作的默认连接的地方。当然,您可以使用管理器类同时使用多个连接。此设置的默认值为 main
。
Channels 连接
此选项 connections
是为您的应用程序设置的每个连接。已包含示例配置,但您可以添加任意数量的连接。
加密通道
要启用端到端加密通道,您需要取消注释 Channels 配置文件中的一行
'app_id' => env('APP_ID'),
'options' => [
'cluster' => env('APP_CLUSTER'),
'encryption_master_key' => env('ENCRYPTION_MASTER_KEY'),
],
'host' => null,
'port' => null,
然后您需要在您的 .env
文件中设置一个 encryption_master_key
。然后您应该能够将加密事件发布到以 private-encrypted
为前缀的通道,您可以通过检查应用程序的(仪表板)[https://dashboard.pusher.com] 调试控制台来验证这是否正常工作!
使用
PusherManager
这是最感兴趣的类。它被绑定到ioc容器上的pusher
,并且可以通过Facades\Pusher
外观来访问。这个类通过扩展AbstractManager实现了ManagerInterface。接口和抽象类都是Graham Campbell的Laravel Manager包的一部分,所以你可能想查看该仓库中的文档,了解如何使用管理器类。注意,返回的连接类将始终是Pusher
的实例。
Facades\Pusher
这个外观将动态地将静态方法调用传递到ioc容器中的pusher
对象,默认情况下是PusherManager
类。
PusherServiceProvider
这个类不包含有趣的公共方法。这个类应该添加到config/app.php
中的providers数组。这个类将设置ioc绑定。
示例
在这里,你可以看到这个包是多么简单易用。默认情况下,默认适配器是main
。在你将认证详情输入到配置文件后,它就会正常工作
// You can alias this in config/app.php. use Pusher\Laravel\Facades\Pusher; Pusher::trigger('my-channel', 'my-event', ['message' => $message]); // We're done here - how easy was that, it just works! Pusher::getSettings(); // This example is simple and there are far more methods available.
PusherManager
的行为就像是一个Pusher
。如果你想调用特定的连接,你可以使用连接方法来做到这一点
use Pusher\Laravel\Facades\Pusher; // Writing this… Pusher::connection('main')->log('They see me logging…'); // …is identical to writing this Pusher::log('They hatin…'); // and is also identical to writing this. Pusher::connection()->log('Tryin to catch me testing dirty…'); // This is because the main connection is configured to be the default. Pusher::getDefaultConnection(); // This will return main. // We can change the default connection. Pusher::setDefaultConnection('alternative'); // The default is now alternative.
如果你像我一样喜欢使用依赖注入而不是外观,那么你可以注入管理器
use Pusher\Laravel\PusherManager; class Foo { protected $pusher; public function __construct(PusherManager $pusher) { $this->pusher = $pusher; } public function bar() { $this->pusher->trigger('my-channel', 'my-event', ['message' => $message]); } } App::make('Foo')->bar();
文档
这个包中还有一些没有在这里记录的类。这是因为这个包是官方Channels包的Laravel包装。