phpsir / websockets-bundle
Symfony 4 的 Ratchet 抽象层
v2.1
2019-12-06 15:13 UTC
Requires
- php: ^7.3
- cboden/ratchet: ^0.4.1
- ratchet/pawl: ^0.3.4
- symfony/framework-bundle: ^3.4|^4.3|^5.0
This package is not auto-updated.
Last update: 2024-09-28 13:52:26 UTC
README
这个小巧的包是 Symfony 4 的 Ratchet 抽象层。只需创建消息处理器,服务器就设置好了。
安装
打开命令行,进入您的项目目录并执行
$ composer require phpsir/websockets-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 */
}
}
作者
Gonzalo Alonso - gonkpo@gmail.com
通过
Pierre Rolland - roll.pierre@gmail.com