rollandrock / websocket-bundle
Symfony 4的Ratchet抽象
dev-master
2018-01-08 16:34 UTC
Requires
- php: ^7.0
- cboden/ratchet: ^0.4.1
- symfony/framework-bundle: ~4.0
This package is auto-updated.
Last update: 2024-09-18 01:26:24 UTC
README
这个小型包是Symfony 4的Ratchet抽象。只需创建消息处理器,您的服务器就设置好了。
安装
打开命令行,进入您的项目目录并执行
$ composer require rollandrock/websocket-bundle
创建处理器
创建实现HandlerInterface
接口的服务
<?php // src/Handler/WelcomeHandler.php namespace App\Handler; use Ratchet\ConnectionInterface; use RollandRock\WebsocketBundle\Client\ClientStack; use RollandRock\WebsocketBundle\Handler\HandlerInterface; class WelcomeHandler implements HandlerInterface { public static function getName(): string { return 'welcome'; } public function handle(ClientStack $clientStack, ConnectionInterface $from, array $data) { // Handle the "welcome" message sent by $from, containing $data. // You also have access to the whole clients stack } }
配置处理器
默认端口号是4242。您可以更改它。此外,将提供的默认客户端将是RollandRock\WebsocketBundle\Client
的实例。您可以扩展它以适应您的需求并在配置中指定它。
rolland_rock_websocket: port: 3240 client: App\Client\Client
运行服务器
php bin/console rr:websocket:server
发送消息
消息需要以下格式
{ "type": "welcome", "data": { /* some data */ } }