phrity / websocket
WebSocket 客户端和服务器
3.1.0
2024-07-09 11:00 UTC
Requires
- php: ^8.1
- phrity/net-stream: ^2.0
- phrity/net-uri: ^2.1
- phrity/util-errorhandler: ^1.1
- psr/http-message: ^1.1 | ^2.0
- psr/log: ^1.0 | ^2.0 | ^3.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^10.0 | ^11.0
- phrity/net-mock: ^2.0
- squizlabs/php_codesniffer: ^3.5
- dev-v3.0-main
- dev-v3.1-main
- dev-v2.2-main
- 3.1.0
- 3.0.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.1
- 0.1.0
- dev-v3.2-main
This package is auto-updated.
Last update: 2024-09-17 17:21:14 UTC
README
此库包含用于PHP的WebSocket客户端和服务器。替代 textalk/websocket
。
客户端和服务器提供用于读取和写入WebSocket流的函数。
此仓库替代了已废弃的 textalk/websocket
仓库,并由Sören Jensen维护,他自 v1.3
版本以来一直在维护原始版本。
文档
安装
首选的安装方法是使用 Composer。
composer require phrity/websocket
客户端
客户端 可在WebSocket流上读取和写入。它内部支持升级握手和隐式关闭以及ping/pong操作。
为请求/响应策略设置WebSocket客户端
$client = new WebSocket\Client("wss://echo.websocket.org/"); $client // Add standard middlewares ->addMiddleware(new WebSocket\Middleware\CloseHandler()) ->addMiddleware(new WebSocket\Middleware\PingResponder()) ; // Send a message $client->text("Hello WebSocket.org!"); // Read response (this is blocking) $message = $client->receive(); echo "Got message: {$message->getContent()} \n"; // Close connection $client->close();
为连续订阅设置WebSocket客户端
$client = new WebSocket\Client("wss://echo.websocket.org/"); $client // Add standard middlewares ->addMiddleware(new WebSocket\Middleware\CloseHandler()) ->addMiddleware(new WebSocket\Middleware\PingResponder()) // Listen to incoming Text messages ->onText(function (WebSocket\Client $client, WebSocket\Connection $connection, WebSocket\Message\Message $message) { // Act on incoming message echo "Got message: {$message->getContent()} \n"; // Possibly respond to server $client->text("I got your your message"); }) ->start();
服务器
服务器 是一个多连接监听服务器。它内部支持升级握手和隐式关闭以及ping/pong操作。
为连续监听设置WebSocket服务器
$server = new WebSocket\Server(); $server // Add standard middlewares ->addMiddleware(new WebSocket\Middleware\CloseHandler()) ->addMiddleware(new WebSocket\Middleware\PingResponder()) // Listen to incoming Text messages ->onText(function (WebSocket\Server $server, WebSocket\Connection $connection, WebSocket\Message\Message $message) { // Act on incoming message echo "Got message: {$message->getContent()} \n"; // Possibly respond to client $connection->text("I got your your message"); }) ->start();