diff --git a/README.md b/README.md index c83f320..e11980a 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ API 文档:[https://apidoc.gitee.com/yurunsoft/YurunHttp](https://apidoc.gitee > 每个小版本的更新日志请移步到 Release 查看 +v5.0.1 支持 WebSocket 压缩及指定 Opcode + v5.0.0 支持 `psr/http-message` `~2.0` 版本 (PHP >= 7.1) v4.3.0 新增支持连接池 @@ -230,12 +232,19 @@ $config = $pool->getConfig(); go(function(){ $url = 'ws://127.0.0.1:1234/'; $http = new HttpRequest; + // 启用压缩 + // $http->websocketCompression(true); $client = $http->websocket($url); if(!$client->isConnected()) { throw new \RuntimeException('Connect failed'); } + // 文本 $client->send('data'); + // 二进制 + $client->send('data', \Yurun\Util\YurunHttp\WebSocket\Opcode::BINARY); + // 更多 Opcode 预定义在:\Yurun\Util\YurunHttp\WebSocket\Opcode + // 也支持 WebSocket 标准中的其它 Opcode $recv = $client->recv(); var_dump('recv:', $recv); $client->close(); diff --git a/src/HttpRequest.php b/src/HttpRequest.php index f3fcd9f..5129a10 100644 --- a/src/HttpRequest.php +++ b/src/HttpRequest.php @@ -219,6 +219,13 @@ class HttpRequest */ public $connectionPool; + /** + * 是否启用 WebSocket 压缩. + * + * @var bool + */ + public $websocketCompression = false; + /** * 代理认证方式. * @@ -843,6 +850,18 @@ public function method($method) return $this; } + /** + * 设置是否启用 WebSocket 压缩. + * + * @return static + */ + public function websocketCompression(bool $websocketCompression): self + { + $this->websocketCompression = $websocketCompression; + + return $this; + } + /** * 设置是否启用连接池. * @@ -950,6 +969,7 @@ public function buildRequest($url = null, $requestBody = null, $method = null, $ ->withAttribute(Attributes::CONNECTION_POOL, $this->connectionPool) ->withAttribute(Attributes::RETRY, $this->retry) ->withAttribute(Attributes::RETRY_CALLBACK, $this->retryCallback) + ->withAttribute(Attributes::WEBSOCKET_COMPRESSION, $this->websocketCompression) ->withProtocolVersion($this->protocolVersion) ; foreach ($this->proxy as $name => $value) diff --git a/src/YurunHttp/Attributes.php b/src/YurunHttp/Attributes.php index cac9a3d..010f12e 100644 --- a/src/YurunHttp/Attributes.php +++ b/src/YurunHttp/Attributes.php @@ -179,6 +179,11 @@ abstract class Attributes */ const CONNECTION_POOL = 'connection_pool'; + /** + * WebSocket 压缩. + */ + const WEBSOCKET_COMPRESSION = 'websocket_compression'; + /** * 重试计数. */ diff --git a/src/YurunHttp/WebSocket/IWebSocketClient.php b/src/YurunHttp/WebSocket/IWebSocketClient.php index 7fcb978..456f5da 100644 --- a/src/YurunHttp/WebSocket/IWebSocketClient.php +++ b/src/YurunHttp/WebSocket/IWebSocketClient.php @@ -57,7 +57,7 @@ public function close(); * * @return bool */ - public function send($data); + public function send($data, int $opcode = Opcode::TEXT); /** * 接收数据. @@ -95,4 +95,9 @@ public function getErrorMessage(); * @return mixed */ public function getClient(); + + /** + * 是否启用压缩. + */ + public function isCompressed(): bool; } diff --git a/src/YurunHttp/WebSocket/Opcode.php b/src/YurunHttp/WebSocket/Opcode.php new file mode 100644 index 0000000..243339c --- /dev/null +++ b/src/YurunHttp/WebSocket/Opcode.php @@ -0,0 +1,18 @@ +request = $request; $this->response = $response; $this->handler = $request->getAttribute(Attributes::PRIVATE_CONNECTION); + $this->compressed = $request->getAttribute(Attributes::WEBSOCKET_COMPRESSION); $this->connected = true; } @@ -120,10 +128,10 @@ public function close() * * @return bool */ - public function send($data) + public function send($data, int $opcode = Opcode::TEXT) { $handler = $this->handler; - $result = $handler->push($data); + $result = $handler->push($data, $opcode, \SWOOLE_WEBSOCKET_FLAG_FIN | ($this->compressed ? \SWOOLE_WEBSOCKET_FLAG_COMPRESS : 0)); if (!$result) { $errCode = $handler->errCode; @@ -190,4 +198,12 @@ public function getClient() { return $this->handler; } + + /** + * 是否启用压缩. + */ + public function isCompressed(): bool + { + return $this->compressed; + } }