kanata-php / conveyor-server-client
为 Socket Conveyor 准备的服务端 WebSocket 客户端
0.1.6
2024-09-30 03:49 UTC
Requires
- php: ^8.2
- textalk/websocket: ^1.5
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.32.0
- kanata-php/socket-conveyor: ^3.0.1-beta
- openswoole/core: ^22.1
- openswoole/ide-helper: ^22.0
- phpunit/phpunit: ^9.5
README
PHP包 kanata-php/socket-conveyor 的服务端客户端
先决条件
- PHP >= 8.0
- PHP OpenSwoole 扩展
安装
composer require kanata-php/conveyor-server-client
描述
这是一个为 Socket Conveyor PHP 包提供的服务端客户端。
使用方法
安装完成后,以下示例展示了如何使用此包。
use Kanata\ConveyorServerClient\Client; $options = [ 'onMessageCallback' => function(Client $currentClient, string $message) { echo 'Message received: ' . $message . PHP_EOL; }, ]; $client = new Client($options); $client->connect();
在前一个示例中,您将有一个连接并等待消息的 PHP 脚本。对于收到的每条消息,执行此脚本的终端都会有打印消息。此客户端将尝试连接到 ws://127.0.0.1:8000
。要了解更多关于 Socket Conveyor 通道和监听器的信息,您可以查阅其 文档。
重要:此示例没有 超时。这意味着它将一直运行,直到进程被杀死。如果您只需要监听有限的时间,或出于任何其他原因需要超时,请使用
timeout
选项。
此包有以下选项(显示各自的默认值)
[ /** * @var string */ 'protocol' => 'ws', /** * @var string */ 'uri' => '127.0.0.1', /** * @var int */ 'port' => 8000, /** * @var string */ 'query' => '', /** * @var ?string */ 'channel' => null, /** * @var ?string */ 'listen' => null, /** * @var ?callable */ 'onOpenCallback' => null, /** * @var ?callable */ 'onReadyCallback' => null, /** * Callback for incoming messages. * Passed parameters: * - \WebSocket\Client $client * - string $message * * @var ?callable */ 'onMessageCallback' => null, /** * Callback for disconnection. * Passed parameters: * - \WebSocket\Client $client * - int $reconnectionAttemptsCount * * @var ?callable */ 'onDisconnectCallback' => null, /** * Callback for Reconnection moment. * Passed parameters: * - \WebSocket\Client $client * - int \Throwable $e * * @var ?callable */ 'onReconnectionCallback' => null, /** * When positive, considered in seconds * * @var int */ 'timeout' => -1, /** * @var bool */ 'reconnect' => false; /** * Number of attempts if disconnects * For this to keeps trying forever, set it to -1. * * @var int */ 'reconnectionAttempts' => = 0; /** * Interval to reconnect in seconds * * @var int */ 'reconnectionInterval' => = 2; ]
这是此包的 Conveyor 客户端接口
namespace Kanata\ConveyorServerClient; use WebSocket\Client; interface ClientInterface { public function connect(): void; public function getClient(): ?Client; public function close(): void; public function send(string $message): void; public function sendRaw(string $message): void; }
作者
👤 Savio Resende
- 网站: https://savioresende.com.br
- GitHub: @lotharthesavior
📝 许可证
版权 © 2022 Savio Resende。