-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 优化 MemoryStream 性能 * 优化代码 * 支持 SSE 服务端推送 * 增加测试用例和文档 * 修复测试 * 更新文档 * 更新文档 * 补充响应头 * 为 IEmitHandler::send() 增加 bool 返回值 * 更新文档
- Loading branch information
Showing
35 changed files
with
723 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# SSE | ||
|
||
[TOC] | ||
|
||
## SSE 介绍 | ||
|
||
SSE 是一种服务端主动向客户端(浏览器)推送数据的技术。 | ||
|
||
大名鼎鼎的 ChatGPT 的 API 接口就用了这项技术,实现逐字返回的打字机效果。 | ||
|
||
服务端向客户端发送一个响应头:`Content-Type: text/event-stream` | ||
|
||
然后服务端按如下格式发送数据: | ||
|
||
```text | ||
: 注释 | ||
data: 数据\n | ||
event: 事件\n | ||
id: id值\n | ||
retry: 重试时间间隔,单位:秒\n\n | ||
``` | ||
|
||
> 其中每一行都是非必传项,每一行必须以 `\n` 结尾 | ||
> `\n\n` 代表一次推送的结束 | ||
## 环境支持 | ||
|
||
| 名称 | 是否支持 | 备注 | ||
| -|-|- | ||
| Swoole | ✔ | | | ||
| Workerman | ✔ | | | ||
| php-fpm | ✔ | `php -S` 暂时有 BUG,`php-fpm` 可用。 | | ||
| RoadRunner | ✖ | 暂时无法实现 | | ||
|
||
## 使用示例 | ||
|
||
```php | ||
use Imi\Server\Http\Message\Emitter\SseEmitter; | ||
use Imi\Server\Http\Message\Emitter\SseMessageEvent; | ||
|
||
/** | ||
* SSE. | ||
* | ||
* @Action | ||
*/ | ||
public function sse(): void | ||
{ | ||
$this->response->setResponseBodyEmitter(new class() extends SseEmitter { | ||
protected function task(): void | ||
{ | ||
$handler = $this->getHandler(); | ||
// 模拟推送数据 | ||
foreach (range(1, 100) as $i) | ||
{ | ||
// 推送数据 | ||
$handler->send((string) new SseMessageEvent((string) $i)); | ||
usleep(10000); | ||
} | ||
} | ||
}); | ||
} | ||
``` | ||
|
||
### SseMessageEvent | ||
|
||
`Imi\Server\Http\Message\Emitter\SseMessageEvent` 类是 SSE 推送事件类,构造方法参数如下: | ||
|
||
```php | ||
public function __construct( | ||
?string $data = null, | ||
?string $event = null, | ||
?string $id = null, | ||
?int $retry = null, | ||
?string $comment = null | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Imi\Fpm\Http\Message; | ||
|
||
use Imi\Server\Http\Message\Emitter\Handler\IEmitHandler; | ||
|
||
class FpmEmitHandler implements IEmitHandler | ||
{ | ||
public function send(string $data): bool | ||
{ | ||
echo $data; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Components/swoole/src/Http/Message/SwooleEmitHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Imi\Swoole\Http\Message; | ||
|
||
use Imi\Server\Http\Message\Emitter\Handler\IEmitHandler; | ||
use Swoole\Http\Response; | ||
|
||
class SwooleEmitHandler implements IEmitHandler | ||
{ | ||
private Response $response; | ||
|
||
public function __construct(Response $response) | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
public function send(string $data): bool | ||
{ | ||
return (bool) $this->response->write($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.