laupifrpar / pusher-bundle
Symfony2 的 Pusher 扩展包
Requires
- php: >=8.0.2
- ext-json: *
- pusher/pusher-php-server: ^7.0
- symfony/config: ^5.4|^6.0
- symfony/dependency-injection: ^5.4|^6.0
- symfony/http-kernel: ^5.4|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- symfony/framework-bundle: ^5.4|^6.0
- symfony/phpunit-bridge: ^5.4|^6.0
- symfony/twig-bundle: ^5.4|^6.0
README
此扩展包使您能简单地使用 Pusher。
Pusher (文档) 是一个简单的托管 API,通过 WebSockets 向 Web 和移动应用程序或任何其他互联网连接设备添加实时双向功能。它非常强大,非常有趣!
此扩展包遵循 MIT 许可。
安装
使用 composer 安装此扩展包。
composer require laupifrpar/pusher-bundle
如果您不使用 Symfony Flex,则还需要在您的 config/bundles.php
文件中启用 Lopi\Bundle\PusherBundle\LopiPusherBundle
。
<?php return [ // ... Lopi\Bundle\PusherBundle\LopiPusherBundle::class => ['all' => true], // ... ];
配置
如果您还没有 Pusher 账户,请在继续之前注册并记下您的 API 密钥。
通用
开始之前,您需要设置一些配置。
这是 yml 中的默认配置
# app/config/config.yml lopi_pusher: # Default configuration scheme: http host: api.pusherapp.com port: 80 cluster: us-east-1 # Change the cluster name timeout: 30 debug: false # true if you want use the debug of all requests
您必须设置 url
参数
# app/config/config.yml lopi_pusher: url: <scheme>://<key>:<secret>@<host>[:<port>]/apps/<app-id>
它将解析 URL 并设置或替换默认值,包括各种参数 scheme
、key
、secret
、host
、port
和 app_id
或者,您可以分别设置各种参数
# app/config/config.yml lopi_pusher: app_id: <app-id> key: <key> secret: <secret>
默认情况下,调用将通过非加密连接进行。要将其更改为通过 HTTPS 进行调用,只需
# app/config/config.yml lopi_pusher: # ... scheme: https port: 443
如果您想使用私有或存在频道,设置参数 auth_service_id
# app/config/config.yml lopi_pusher: # ... auth_service_id: <the_auth_service_id>
请参阅下文关于 "私有和存在频道认证" 的部分
用法!
配置完扩展包后,您将可以访问一个 Pusher 服务,该服务可以通过 Pusher\Pusher
类型提示自动注入。在控制器内部,您可以使用它如下
use Pusher\Pusher; class SampleController { public function triggerPusherAction(Pusher $pusher) { // ... $data['message'] = 'hello world'; $pusher->trigger('test_channel', 'my_event', $data); // ... } }
此代码将自动注入来自官方 Pusher SDK 的 \Pusher\Pusher
类。您可以在 Pusher 文档 中找到所有相关信息。
私有和存在频道认证(可选)
如果您想使用私有或存在频道,需要添加一个授权服务。
首先,创建一个实现 Lopi\Bundle\PusherBundle\Authenticator\ChannelAuthenticatorInterface
的授权服务。
<?php // src/Pusher/ChannelAuthenticator.php namespace App\Pusher; use Lopi\Bundle\PusherBundle\Authenticator\ChannelAuthenticatorInterface; class ChannelAuthenticator implements ChannelAuthenticatorInterface { public function authenticate($socketId, $channelName) { // logic here return true; } }
然后,像平常一样注册它作为服务
# config/services.yml services: my_channel_authenticator: AppBundle\Pusher\ChannelAuthenticator
然后将其 服务 ID 包含在 lopi_pusher 的 auth_service_id
配置参数中
# config/packagers/lopi_pusher.yml lopi_pusher: # ... auth_service_id: 'my_channel_authenticator'
此外,通过向您的 config\routing.yml
配置中添加以下内容来启用路由
# config\routing.yml lopi_pusher: resource: "@LopiPusherBundle/Resources/config/routing.xml" prefix: /pusher
在某些 Symfony 配置中,您可能需要手动指定 channel_auth_endpoint
:在大多数设置中不需要
{# app/Resources/views/base.html.twig #} <script type="text/javascript"> Pusher.channel_auth_endpoint = "{{ path('lopi_pusher_bundle_auth') }}"; </script>
报告问题或功能请求
问题和功能请求在 Github 问题跟踪器 中跟踪。
在报告错误时,最好在基于 Symfony Standard Edition 的基本项目中重现错误,以便扩展包的开发者可以通过克隆它并遵循一些步骤来重现问题。