amphp/websocket-client
Composer 安装命令:
composer require amphp/websocket-client
包简介
Async WebSocket client for PHP based on Amp.
README 文档
README
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
amphp/websocket-client provides an asynchronous WebSocket client for PHP based on Amp.
Websockets are full-duplex communication channels, which are mostly used for realtime communication where the HTTP request / response cycle has too much overhead.
They're also used if the server should be able to push data to the client without an explicit request.
There are various use cases for a WebSocket client in PHP, such as consuming realtime APIs, writing tests for a WebSocket server, or controlling web browsers via their remote debugging APIs, which are based on WebSockets.
Installation
This package can be installed as a Composer dependency.
composer require amphp/websocket-client
Requirements
- PHP 8.1+
Usage
Connecting
You can create new WebSocket connections using Amp\Websocket\connect() or calling connect() on an instance of WebsocketConnector.
The connect() function accepts a string, PSR-7 UriInterface instance, or a WebsocketHandshake as first argument. URIs must use the ws or wss (WebSocket over TLS) scheme.
Custom connection parameters can be specified by passing a WebsocketHandshake object instead of a string as first argument, which can also be used to pass additional headers with the initial handshake. The second argument is an optional Cancellation which may be used to cancel the connection attempt.
<?php require 'vendor/autoload.php'; use function Amp\Websocket\Client\connect; // Amp\Websocket\Client\connect() uses the WebsocketConnection instance // defined by Amp\Websocket\Client\websocketConnector() $connection = connect('ws://localhost:1337/websocket'); foreach ($connection as $message) { // $message is an instance of Amp\Websocket\WebsocketMessage }
Custom Connection Parameters
If necessary, a variety of connection parameters and behaviors may be altered by providing a customized instance of WebsocketConnectionFactory to the WebsocketConnector used to establish a WebSocket connection.
use Amp\Websocket\Client\Rfc6455ConnectionFactory; use Amp\Websocket\Client\Rfc6455Connector; use Amp\Websocket\Client\WebsocketHandshake; use Amp\Websocket\ConstantRateLimit; use Amp\Websocket\Parser\Rfc6455ParserFactory; use Amp\Websocket\PeriodicHeartbeatQueue; $connectionFactory = new Rfc6455ConnectionFactory( heartbeatQueue: new PeriodicHeartbeatQueue( heartbeatPeriod: 5, // 5 seconds ), rateLimit: new ConstantRateLimit( bytesPerSecondLimit: 2 ** 17, // 128 KiB framesPerSecondLimit: 10, ), parserFactory: new Rfc6455ParserFactory( messageSizeLimit: 2 ** 20, // 1 MiB ), frameSplitThreshold: 2 ** 14, // 16 KiB closePeriod: 0.5, // 0.5 seconds ); $connector = new Rfc6455Connector($connectionFactory); $handshake = new WebsocketHandshake('wss://example.com/websocket'); $connection = $connector->connect($handshake);
Sending Data
WebSocket messages can be sent using the Connection::sendText() and Connection::sendBinary() methods.
Text messages sent with Connection::sendText() must be valid UTF-8.
Binary messages send with Connection::sendBinary() can be arbitrary data.
Both methods return as soon as the message has been fully written to the send buffer. This does not mean that the message is guaranteed to have been received by the other party.
Receiving Data
WebSocket messages can be received using the Connection::receive() method. Connection::receive() returns a WebsocketMessage instance once the client has started to receive a message. This allows streaming WebSocket messages, which might be pretty large. In practice, most messages are rather small, and it's fine buffering them completely by either calling WebsocketMessage::buffer() or casting the object to a string. The maximum length of a message is defined by the option given to the WebsocketParserFactory instance provided to the WebsocketConnectionFactory (10 MiB by default).
use Amp\Websocket\Client\WebsocketHandshake; use Amp\Websocket\WebsocketCloseCode; use function Amp\Websocket\Client\connect; // Connects to the websocket endpoint at libwebsockets.org // which sends a message every 50ms. $handshake = (new WebsocketHandshake('wss://libwebsockets.org')) ->withHeader('Sec-WebSocket-Protocol', 'dumb-increment-protocol'); $connection = connect($handshake); foreach ($connection as $message) { $payload = $message->buffer(); printf("Received: %s\n", $payload); if ($payload === '100') { $connection->close(); break; } }
Versioning
amphp/websocket-client follows the semver semantic versioning specification like all other amphp packages.
Security
If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.
License
The MIT License (MIT). Please see LICENSE for more information.
amphp/websocket-client 适用场景与选型建议
amphp/websocket-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.94M 次下载、GitHub Stars 达 163, 最近一次更新时间为 2018 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「websocket」 「client」 「async」 「non-blocking」 「amp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 amphp/websocket-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amphp/websocket-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 amphp/websocket-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
http客户端
WebSocket view engine
Easily add Excepct-CT header to your project
统计信息
- 总下载量: 4.94M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 165
- 点击次数: 29
- 依赖项目数: 52
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-12