totoro1302/php-websocket-client

PHP Websocket 客户端

v0.1.7 2023-08-15 23:45 UTC

This package is auto-updated.

Last update: 2024-09-03 21:09:00 UTC


README

这是什么?

遵循RFC6455的PHP Websocket客户端的基本实现

安装

如果您想在项目中安装它,请通过composer引入

composer require totoro1302/php-websocket-client

测试

PHPUnit

  • src/ 上覆盖率必须达到100%

堆栈描述

PHP

  • 需要PHP >= 8.2

扩展

  • 扩展 pcntl

依赖

  • psr/log
  • nyholm/psr7
  • psr/http-factory

运行测试

运行PHP单元测试

bin/test.sh unit

运行代码检查器

bin/test.sh static

运行PHPStan静态分析

bin/test.sh static-analyze

运行PHP 8.2兼容性测试

bin/test.sh php82-compatibility

运行代码异味修复

bin/test.sh static-fix

用法

<?php

use Totoro1302\PhpWebsocketClient\Client;use Totoro1302\PhpWebsocketClient\ClientConfig;

$clientConfig = new ClientConfig(
'myWsClient', // give a name to the connection (mandatory)
'wss://some-ws-srv.com', // websocket server address (mandatory)
10, // connection timeout (optional)
'some_origin.com', // you can specify an origin (optional)
false, // persistent (optional)
['user-agent' => 'myAgent'] // add additional headers (optional)
);

$client = new Client($clientConfig);
$client->connect();
$client->push('Hello world');

while ($client->isRunning()) {
    $data = $client->pull();
    // Do something with the data here
    usleep(mt_rand(50000, 100000)); // You can eventually add some timeout pull delay if needed
}

自动响应

客户端处理两端的ping/pong逻辑。这意味着它会自动用pong帧响应ping帧,客户端也会向服务器发送ping帧以检查服务器是否始终处于活跃状态。