定制 valtzu/guzzle-websocket-middleware 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

valtzu/guzzle-websocket-middleware

最新稳定版本:0.3.0

Composer 安装命令:

composer require valtzu/guzzle-websocket-middleware

包简介

README 文档

README

NOTE: Only StreamHandler supported (so not CurlHandler!).

Installation

composer require valtzu/guzzle-websocket-middleware

Usage

Guzzle's synchronous option is used to configure the stream blocking option.

Synchronous usage

$handlerStack = new HandlerStack(new StreamHandler());
$handlerStack->unshift(new WebSocketMiddleware());

$guzzle = new Client(['handler' => $handlerStack]);

$handshakeResponse = $guzzle->request('GET', 'wss://ws.postman-echo.com/raw');
$ws = $handshakeResponse->getBody();

$ws->write("Hello world");
$helloWorld = $ws->read(); // This will block until the reply frame is received

Asynchronous usage

$handlerStack = new HandlerStack(new StreamHandler());
$handlerStack->unshift(new WebSocketMiddleware());

$guzzle = new Client(['handler' => $handlerStack]);

$handshakeResponse = $guzzle->requestAsync('GET', 'wss://ws.postman-echo.com/raw')->wait();
$ws = $handshakeResponse->getBody();

$ws->write("Hello world");
$helloWorld = $ws->read(); // Here you may get an empty string if data wasn't received yet

Connection upkeep / ping-pong

Whenever you read from a websocket stream, instead of receiving a text/binary frame, you may receive "ping" instead. When this happens, we automatically respond with "pong". However, due to being PHP (usually) being single-threaded, this means that you must make sure read (even with 0 length) is done frequently enough.

It's also possible to manually send a ping – however, it does not wait for the other party to reply.

$ws->ping();

Contributing

  1. If you have an issue with package, or you have a feature request, please open an issue
  2. If you want to contribute code, you may submit a Pull Request. Just remember to add tests.

统计信息

  • 总下载量: 13.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-10