v4.6.2 版本主要是一个 Bug 修复版本,没有向下不兼容改动。
新增了 CoroutineSocket->recvLine() 和 CoroutineSocket->readWithBuffer() 方法
分别用于解决 socket_read 兼容性问题和使用 recv(1) 逐字节接收时产生大量系统调用问题
同时增强了 Responsecreate() 方法,可以独立于 Server 使用,如:
`use SwooleCoroutineServer;
use SwooleCoroutineServerConnection;
use SwooleHttpRequest;
use SwooleHttpResponse;
SwooleCoroutinerun(function () {
$server = new Server('0.0.0.0', 9501, false);
go(function () use ($server) {
$server->handle(function (Connection $conn) use ($server) {
$req = Request::create();
while(true) {
$data = $conn->recv();
if (strlen($data) != $req->parse($data) or $req->isCompleted()) {
break;
}
}
var_dump($req->get);
$resp = Response::create([$conn->exportSocket(), $req]);
$resp->header('X-Server', 'swoole');
$resp->end('Hello, Swoole');
$server->shutdown();
});
$server->start();
});
});
`
启动后使用 curl 发起请求
`$ curl -I http://127.0.0.1:9501/?hello=swoole
HTTP/1.1 200 OK
X-Server: swoole
Server: swoole-http-server
Connection: keep-alive
Content-Type: text/html
Date: Mon, 25 Jan 2021 10:58:31 GMT
Content-Length: 13
$ curl http://127.0.0.1:9501/?hello=swoole
Hello, Swoole
`
而终端会打印请求中的 GET 参数
`array(1) {
["hello"]=>
string(6) "swoole"
}
`
下面是完整的更新日志:
新增 API
- 新增 HttpRequestgetMethod() 方法 (#3987) (@luolaifa000)
- 新增 CoroutineSocket->recvLine() 方法 (#4014) (@matyhtf)
- 新增 CoroutineSocket->readWithBuffer() 方法 (#4017) (@matyhtf)
增强
- 增强 Responsecreate() 方法,可以独立于 Server 使用 (#3998) (@matyhtf)
- 支持 CoroutineRedis->hExists 在设置了 compatibility_mode 之后返回 bool 类型 (swoole/swoole-src@b8cce7c) (@matyhtf)
- 支持 socket_read 设置 PHP_NORMAL_READ 选项 (swoole/swoole-src@b1a0dcc) (@matyhtf)
修复
- 修复 Coroutine::defer 在 PHP8 下 coredump 的问题 (#3997) (@huanghantao)
- 修复当使用 thread context 的时候,错误设置 CoroutineSocket::errCode 的问题 (swoole/swoole-src@004d08a) (@matyhtf)
- 修复在最新的 macos 下 Swoole 编译失败的问题 (#4007) (@matyhtf)
- 修复当 md5_file 参数传入 url 导致 php stream context 为空指针的问题 (#4016) (@ZhiyangLeeCN)
内核
-
使用 AIO 线程池 hook stdio(解决之前把 stdio 视为 socket 导致的多协程读写问题) (#4002) (@matyhtf)
-
重构 HttpContext (#3998) (@matyhtf)
-
重构 Process::wait() (#4019) (@matyhtf)