Skip to content

Commit df62abc

Browse files
authored
支持 WebSocket 压缩及指定 Opcode (#29)
* 支持 WebSocket 压缩及指定 Opcode * 更新文档
1 parent 8ba131a commit df62abc

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ API 文档:[https://apidoc.gitee.com/yurunsoft/YurunHttp](https://apidoc.gitee
4646

4747
> 每个小版本的更新日志请移步到 Release 查看
4848
49+
v5.0.1 支持 WebSocket 压缩及指定 Opcode
50+
4951
v5.0.0 支持 `psr/http-message` `~2.0` 版本 (PHP >= 7.1)
5052

5153
v4.3.0 新增支持连接池
@@ -230,12 +232,19 @@ $config = $pool->getConfig();
230232
go(function(){
231233
$url = 'ws://127.0.0.1:1234/';
232234
$http = new HttpRequest;
235+
// 启用压缩
236+
// $http->websocketCompression(true);
233237
$client = $http->websocket($url);
234238
if(!$client->isConnected())
235239
{
236240
throw new \RuntimeException('Connect failed');
237241
}
242+
// 文本
238243
$client->send('data');
244+
// 二进制
245+
$client->send('data', \Yurun\Util\YurunHttp\WebSocket\Opcode::BINARY);
246+
// 更多 Opcode 预定义在:\Yurun\Util\YurunHttp\WebSocket\Opcode
247+
// 也支持 WebSocket 标准中的其它 Opcode
239248
$recv = $client->recv();
240249
var_dump('recv:', $recv);
241250
$client->close();

src/HttpRequest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ class HttpRequest
219219
*/
220220
public $connectionPool;
221221

222+
/**
223+
* 是否启用 WebSocket 压缩.
224+
*
225+
* @var bool
226+
*/
227+
public $websocketCompression = false;
228+
222229
/**
223230
* 代理认证方式.
224231
*
@@ -843,6 +850,18 @@ public function method($method)
843850
return $this;
844851
}
845852

853+
/**
854+
* 设置是否启用 WebSocket 压缩.
855+
*
856+
* @return static
857+
*/
858+
public function websocketCompression(bool $websocketCompression): self
859+
{
860+
$this->websocketCompression = $websocketCompression;
861+
862+
return $this;
863+
}
864+
846865
/**
847866
* 设置是否启用连接池.
848867
*
@@ -950,6 +969,7 @@ public function buildRequest($url = null, $requestBody = null, $method = null, $
950969
->withAttribute(Attributes::CONNECTION_POOL, $this->connectionPool)
951970
->withAttribute(Attributes::RETRY, $this->retry)
952971
->withAttribute(Attributes::RETRY_CALLBACK, $this->retryCallback)
972+
->withAttribute(Attributes::WEBSOCKET_COMPRESSION, $this->websocketCompression)
953973
->withProtocolVersion($this->protocolVersion)
954974
;
955975
foreach ($this->proxy as $name => $value)

src/YurunHttp/Attributes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ abstract class Attributes
179179
*/
180180
const CONNECTION_POOL = 'connection_pool';
181181

182+
/**
183+
* WebSocket 压缩.
184+
*/
185+
const WEBSOCKET_COMPRESSION = 'websocket_compression';
186+
182187
/**
183188
* 重试计数.
184189
*/

src/YurunHttp/WebSocket/IWebSocketClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function close();
5757
*
5858
* @return bool
5959
*/
60-
public function send($data);
60+
public function send($data, int $opcode = Opcode::TEXT);
6161

6262
/**
6363
* 接收数据.
@@ -95,4 +95,9 @@ public function getErrorMessage();
9595
* @return mixed
9696
*/
9797
public function getClient();
98+
99+
/**
100+
* 是否启用压缩.
101+
*/
102+
public function isCompressed(): bool;
98103
}

src/YurunHttp/WebSocket/Opcode.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Yurun\Util\YurunHttp\WebSocket;
4+
5+
class Opcode
6+
{
7+
public const CONTINUATION = 0x0;
8+
9+
public const TEXT = 0x1;
10+
11+
public const BINARY = 0x2;
12+
13+
public const CLOSE = 0x8;
14+
15+
public const PING = 0x9;
16+
17+
public const PONG = 0xA;
18+
}

src/YurunHttp/WebSocket/Swoole.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class Swoole implements IWebSocketClient
4242
*/
4343
private $connected = false;
4444

45+
/**
46+
* 是否启用压缩.
47+
*
48+
* @var bool
49+
*/
50+
private $compressed = false;
51+
4552
/**
4653
* 初始化.
4754
*
@@ -57,6 +64,7 @@ public function init($httpHandler, $request, $response)
5764
$this->request = $request;
5865
$this->response = $response;
5966
$this->handler = $request->getAttribute(Attributes::PRIVATE_CONNECTION);
67+
$this->compressed = $request->getAttribute(Attributes::WEBSOCKET_COMPRESSION);
6068
$this->connected = true;
6169
}
6270

@@ -120,10 +128,10 @@ public function close()
120128
*
121129
* @return bool
122130
*/
123-
public function send($data)
131+
public function send($data, int $opcode = Opcode::TEXT)
124132
{
125133
$handler = $this->handler;
126-
$result = $handler->push($data);
134+
$result = $handler->push($data, $opcode, \SWOOLE_WEBSOCKET_FLAG_FIN | ($this->compressed ? \SWOOLE_WEBSOCKET_FLAG_COMPRESS : 0));
127135
if (!$result)
128136
{
129137
$errCode = $handler->errCode;
@@ -190,4 +198,12 @@ public function getClient()
190198
{
191199
return $this->handler;
192200
}
201+
202+
/**
203+
* 是否启用压缩.
204+
*/
205+
public function isCompressed(): bool
206+
{
207+
return $this->compressed;
208+
}
193209
}

0 commit comments

Comments
 (0)