nexwap/pusher-http-laravel

[已废弃] Laravel 的 Pusher 中继桥

v4.2.4 2020-07-17 09:27 UTC

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');

Build Status StyleCI Coverage Status Latest Version License

安装

使用 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 是为您的应用程序设置每个连接的地方。已包含示例配置,但您可以添加任何数量的连接。

加密通道

要启用 端到端加密通道,您需要从通道配置文件中取消注释一行

'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.

如果您像我一样更喜欢使用依赖注入而不是外观(facade),则可以注入管理器。

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包装器。

许可证

MIT © Pusher