gamma / pushpin-bundle
Pushpin 代理集成用于 Symfony 应用程序
Requires
- ext-curl: *
- fanout/gripcontrol: ^2.0
- jms/serializer: ^1.1@dev
- sensio/framework-extra-bundle: ^5.1
- symfony/framework-bundle: ~2.3|~3.4
Requires (Dev)
- phpunit/phpunit: ^4.7
This package is not auto-updated.
Last update: 2024-09-19 02:59:07 UTC
README
Symfony 扩展包,帮助你使用 Pushpin 反向代理为应用程序添加实时功能。集成了 php-gripcontrol 库。
##功能
- 与 Pushpin 的 WebSocket-over-HTTP 请求兼容
- 反序列化 (使用 jms/serializer) Pushpin 的 TEXT 事件到你的配置中指定的 DTO (事件)
- 使用你的特定处理程序处理 WebSocketEvent
- Pushpin 辅助工具,用于发布到频道、订阅、分离等。
安装
安装一个扩展包
composer require "gamma/pushpin-bundle"
在 app/AppKernel.php
中注册扩展包
public function registerBundles() { return array( // ... new Gamma\Pushpin\PushpinBundle\GammaPushpinBundle() ); }
配置
// config.yml gamma_pushpin: proxy: control_uri: "https://:5561/" # control URI to Pushpin web_socket: json_events: base_namespace: ~
为了测试,你需要在开发机器上安装一个 Pushpin。并在 routes
配置文件中启用 WebSocket-over-HTTP。
* localhost:80,over_http
使用方法
创建一个事件类。扩展 AbstractJsonEvent
。这个类将保存来自 WebSocket 客户端的数据
<?php namespace AppBundle\WebsocketEvents\Chat; use Gamma\Pushpin\PushpinBundle\Events\Base\AbstractJsonEvent; use JMS\Serializer\Annotation\Type as JMS; class ChatMessage extends AbstractJsonEvent { /** * @var string * @JMS("string") */ public $room; /** * @var string * @JMS("string") */ public $comment; }
使用你的新事件更新配置
// config.yml gamma_pushpin: // ... web_socket: json_events: base_namespace: 'AppBundle\WebsocketEvents' mappings: chatMessage: # logical name of your event class: 'Chat\ChatMessage' # base_namespace + clas should give fully qualified class name
通过扩展 AbstractEventHandler
类创建你的事件的处理服务
<?php namespace AppBundle\Services\WebSocket; use Gamma\Pushpin\PushpinBundle\Events\Base\AbstractEvent; use Gamma\Pushpin\PushpinBundle\Handlers\Base\AbstractEventHandler; use Gamma\Pushpin\PushpinBundle\Interfaces\Events\TextEventInterface; use GripControl\WebSocketEvent; class ChatMessageHandler extends AbstractEventHandler { const EVENT_TYPE = TextEventInterface::EVENT_TYPE; /** * {@inheritdoc} */ public function handle(AbstractEvent $event) { //your logic //Example of creating response event to client: //$resultEvent = new WebSocketEvent('TEXT', 'Hello Client'); } }
在这里,你可以返回一个 WebSocketEvent,WebSocketEvent 对象数组,或者包含事件数组的 WebSocketEventsDTO。将处理程序注册为 Symfony 服务
//services.yml services: app.chat_message_handler: class: AppBundle\Services\Websocket\ChatMessageHandler tags: - { name: gamma.pushpin.grip_event_handler, type: chatMessage }
注意:这里的 type: <logicalEventName>
应该与配置中的事件逻辑名称相似。
在控制器中,应将事件传递给 GripControl::encode_websocket_events
函数。所以你需要做的最后一件事是创建一个简单的控制器,让 Pushpin 可以访问。
<?php namespace AppBundle\Controller; use Gamma\Pushpin\PushpinBundle\Controller\GripController; use Gamma\Pushpin\PushpinBundle\DTO\WebSocketEventsDTO; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; class ChatController extends GripController { /** * @Route("/websocket/chat", name="app_websocket_chat_message") * @param Request $request * @param WebSocketEventsDTO $inputEvents * * @ParamConverter("inputEvents", converter="gamma.web_socket.events", options={"format": "json"}) * @return Response */ public function chatMessageAction(Request $request, WebSocketEventsDTO $inputEvents) { return $this->encodeWebSocketEvents( $this->get('gamma.pushpin.grip.events_handler')->handleEvents($inputEvents) ); } }
如果一切正常,你应该可以使用 URL: ws://:7999/websocket/chat
连接到 Pushpin 的 WebSocket 端口(默认为 7999)
你可以使用 wscat 工具进行测试
$ wscat -c ws://:7999/websocket/chat connected (press CTRL+C to quit)
现在客户端可以向你的应用程序发送消息
chatMessage:{"room":"test","comment":"hello Symfony!"}
这将调用你的处理程序并将结果返回给客户端。
使用频道
Pushpin 作为一个发布-订阅服务。因此,你可以将客户端订阅到特定频道并向其发布消息。你的应用程序中可以作为频道使用的东西应该实现一个具有 getChannelName()
方法的 WebSocketChannelInterface
。要发布消息,你还可以使用 \PubControl\PubControl::publish
通过在 gamma.pushpin.pushpin_helper
上调用方法,你可以
- 将客户端订阅到频道
subscribeToChannel($channel)
- 向频道发送消息
sendWsMessageToChannel($channel, $message)
- 从频道取消订阅
unSubscribeFromChannel($channel)
- 断开连接
detachConnection()
有关 Pushpin 的更多信息,请参阅 此处
工作应用程序的示例
TODO
- 与 Pushpin 一起进行 HTTP 流
- 更多文档
- 更多
phpunit