Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持 WebSocket 压缩及指定 Opcode #29

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 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,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();
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;
}
}
Loading