stas-zozulja / pushpin-bundle
为 Symfony 应用程序集成的 Pushpin 代理
Requires
- ext-curl: *
- doctrine/common: ^2.7
- fanout/gripcontrol: ^2.0
- jms/serializer: ^1.1
- sensio/framework-extra-bundle: ^5.1
- symfony/console: ~2.3|~3.4
- symfony/framework-bundle: ~2.3|~3.4
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2024-09-19 16:53:43 UTC
README
Symfony Bundle,帮助您使用 Pushpin 反向代理将实时功能添加到您的应用程序中。集成了 php-gripcontrol 库。
功能
- WebSocket-over-HTTP
- 与来自 Pushpin 的 WebSocket-over-HTTP 请求 兼容
- 反序列化(使用 jms/serializer)来自 Pushpin 的 TEXT 事件到您配置中指定的 DTO(事件)
- 使用您特定的处理器处理 WebSocketEvent
- Pushpin 辅助工具,用于发布到频道、订阅、分离等。
- HTTP 流
- 为 Pushipn 添加特定的响应头,以保持连接的 HTTP GRIP 传输
安装
安装一个包
composer require "stas-zozulja/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
使用(WebSocket-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>
应与配置中的事件逻辑名称相似。
最后,您需要创建一个简单的控制器,Pushpin 将访问它。使用 @PushpinResponse
注解,我们可以仅从处理器返回 WebSocketEventsDto
实例。
<?php namespace AppBundle\Controller; use Gamma\Pushpin\PushpinBundle\DTO\WebSocketEventsDTO; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Gamma\Pushpin\PushpinBundle\Configuration\PushpinResponse; class ChatController extends Controller { /** * @Route("/websocket/chat", name="app_websocket_chat_message") * * @param WebSocketEventsDto $inputEvents * * @PushpinResponse(format="ws-message") * @ParamConverter("inputEvents", converter="gamma.web_socket.events", options={"format": "json"}) * * @return Response */ public function chatMessageAction(WebSocketEventsDTO $inputEvents) { return $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 的更多信息,请参阅 此处
工作应用程序的示例
待办事项
- HTTP-stream 的文档
- 更多的
phpunit