Skip to content

Commit

Permalink
支持 WebSocket 压缩及指定 Opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jan 17, 2024
1 parent 8ba131a commit 47b3a0d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 新增支持连接池
Expand Down Expand Up @@ -230,6 +232,8 @@ $config = $pool->getConfig();
go(function(){
$url = 'ws://127.0.0.1:1234/';
$http = new HttpRequest;
// 启用压缩
$http->setAttribute('websocket.compression', true);
$client = $http->websocket($url);
if(!$client->isConnected())
{
Expand Down
20 changes: 20 additions & 0 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class HttpRequest
*/
public $connectionPool;

/**
* 是否启用 WebSocket 压缩.
*
* @var bool
*/
public $websocketCompression = false;

/**
* 代理认证方式.
*
Expand Down Expand Up @@ -843,6 +850,18 @@ public function method($method)
return $this;
}

/**
* 设置是否启用 WebSocket 压缩.
*
* @return static
*/
public function websocketCompression(bool $websocketCompression): self
{
$this->websocketCompression = $websocketCompression;

return $this;
}

/**
* 设置是否启用连接池.
*
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/YurunHttp/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ abstract class Attributes
*/
const CONNECTION_POOL = 'connection_pool';

/**
* WebSocket 压缩.
*/
const WEBSOCKET_COMPRESSION = 'websocket_compression';

/**
* 重试计数.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/YurunHttp/WebSocket/IWebSocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function close();
*
* @return bool
*/
public function send($data);
public function send($data, int $opcode = Opcode::TEXT);

/**
* 接收数据.
Expand Down Expand Up @@ -95,4 +95,9 @@ public function getErrorMessage();
* @return mixed
*/
public function getClient();

/**
* 是否启用压缩.
*/
public function isCompressed(): bool;
}
18 changes: 18 additions & 0 deletions src/YurunHttp/WebSocket/Opcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Yurun\Util\YurunHttp\WebSocket;

class Opcode
{
public const CONTINUATION = 0x0;

public const TEXT = 0x1;

public const BINARY = 0x2;

public const CLOSE = 0x8;

public const PING = 0x9;

public const PONG = 0xA;
}
20 changes: 18 additions & 2 deletions src/YurunHttp/WebSocket/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class Swoole implements IWebSocketClient
*/
private $connected = false;

/**
* 是否启用压缩.
*
* @var bool
*/
private $compressed = false;

/**
* 初始化.
*
Expand All @@ -57,6 +64,7 @@ public function init($httpHandler, $request, $response)
$this->request = $request;
$this->response = $response;
$this->handler = $request->getAttribute(Attributes::PRIVATE_CONNECTION);
$this->compressed = $request->getAttribute(Attributes::WEBSOCKET_COMPRESSION);
$this->connected = true;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -190,4 +198,12 @@ public function getClient()
{
return $this->handler;
}

/**
* 是否启用压缩.
*/
public function isCompressed(): bool
{
return $this->compressed;
}
}

0 comments on commit 47b3a0d

Please sign in to comment.