pusher / pusher-http-laravel
[已弃用] Laravel 的 Pusher 中继桥
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 的支持,这是将 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
如果你愿意,可以使用 facade。在 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
。
通道连接
此选项 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
facade 来访问。此类通过扩展 AbstractManager 实现了 ManagerInterface。接口和抽象类都是 Graham Campbell 的 Laravel Manager 包的一部分,因此您可能想前往该存储库查看如何使用管理器类的文档。请注意,返回的连接类始终是 Pusher
的实例。
Facades\Pusher
此 facade 会将静态方法调用动态地传递到 ioc 容器中的 pusher
对象,默认情况下是 PusherManager
类。
PusherServiceProvider
此类不包含任何有趣的可公开访问的方法。此类应添加到 config/app.php
中的提供者数组。此类将设置 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包装。