首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
Swoole v4.6 版本新特性之 Http\Response 增强
在 4.6 版本中,对 `Swoole\Http\Response` 进行了一些增强: - 添加 `Http\Response::isWritable` (db56827) (@matyhtf) - 增强 `Response\create()` 方法,可以独立于 Server 使用 (#3998) (@matyhtf) 再来看一下上一篇文章的示例代码: ```php use Swoole\Server; use Swoole\Http\Request; $server = new Server('127.0.0.1', 9501); $server->on('Receive', function (Server $server, $fd, $reactor_id, $data) { /** @var Request $request */ $request = Request::create(); $request->parse($data); $body = 'Hello, Swoole'; $body_len = strlen($body); $send_data = "HTTP/1.1 200 OK\r\nServer: swoole-server\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: {$body_len}\r\nConnection: keep-alive\r\n\r\n{$body}"; $server->send($fd, $send_data); }); $server->start(); ``` 需要用户自行处理`$send_data`的数据,而此次增强以后,可以这样使用: ```php use Swoole\Server; use Swoole\Http\Request; use Swoole\Http\Response; $server = new Server('127.0.0.1', 9501); $server->on('Receive', function (Server $server, $fd, $reactor_id, $data) { /** @var Request $request */ $request = Request::create(); $request->parse($data); /** @var Response $response */ $response = Response::create($server, $fd); $response->status(200); $response->end('Hello, Swoole'); }); $server->start(); ``` 还可以这样: ```php $server->on('Receive', function (Server $server, $fd, $reactor_id, $data) { /** @var Request $request */ $request = Request::create(); $request->parse($data); /** @var Response $response */ $response = Response::create([$server, $request], $fd); $response->status(200); $response->end('Hello, Swoole'); }); ``` 不但异步 Server 中可以使用,而且协程 Server 中也可以使用: ```php use Swoole\Coroutine\Server; use Swoole\Coroutine\Server\Connection; use Swoole\Http\Request; use Swoole\Http\Response; use function Swoole\Coroutine\run; run(function () { $server = new Server('0.0.0.0', 9501, false); $server->handle(function (Connection $conn) use ($server) { $request = Request::create(); while(true) { $data = $conn->recv(); if (strlen($data) != $request->parse($data) || $request->isCompleted()) { break; } } $response = Response::create([$conn->exportSocket(), $request]); $response->end('Hello, Swoole'); }); $server->start(); }); ``` 这样就可以直接使用 `Http\Response` 的 API 进行响应处理,而不用自行组装 HTTP 协议响应数据。 ## isWritable `isWritable()` 用来判断 `Http\Response` 对象是否已结束 (`end`) 或已分离 (`detach`),例如: ```php use Swoole\Http\Server; use Swoole\Http\Request; use Swoole\Http\Response; $http = new Server('0.0.0.0', 9501); $http->on('request', function (Request $req, Response $resp) { assert($resp->isWritable(), true); $resp->end('hello'); assert($resp->isWritable(), false); $resp->setStatusCode(403); }); $http->start(); ``` 如果在调用`end()`之后,再次调用相关 API,就会发生错误 ```bash PHP Warning: Swoole\Http\Response::setStatusCode(): http response is unavailable (maybe it has been ended or detached) ``` ![](https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/swoole/wechat.png)
发布于3年前 · 4 次浏览 · 来自
分享
鲁飞
在 4.6 版本中,对 `Swoole\Http\Response` 进行了一些增强: - 添加 `Http\Response::isWritable` (db56827) (@matyhtf) - 增强 `Response\create()` 方法,可以独立于 Server 使用 (#3998) (@matyhtf) 再来看一下上一篇文章的示例代码: ```php use Swoole\Server; use Swoole\Http\Request; $server = new Server('127.0.0.1', 9501); $server->on('Receive', function (Server $server, $fd, $reactor_id, $data) { /** @var Request $request */ $request = Request::create(); $request->parse($data); $body = 'Hello, Swoole'; $body_len = strlen($body); $send_data = "HTTP/1.1 200 OK\r\nServer: swoole-server\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: {$body_len}\r\nConnection: keep-alive\r\n\r\n{$body}"; $server->send($fd, $send_data); }); $server->start(); ``` 需要用户自行处理`$send_data`的数据,而此次增强以后,可以这样使用: ```php use Swoole\Server; use Swoole\Http\Request; use Swoole\Http\Response; $server = new Server('127.0.0.1', 9501); $server->on('Receive', function (Server $server, $fd, $reactor_id, $data) { /** @var Request $request */ $request = Request::create(); $request->parse($data); /** @var Response $response */ $response = Response::create($server, $fd); $response->status(200); $response->end('Hello, Swoole'); }); $server->start(); ``` 还可以这样: ```php $server->on('Receive', function (Server $server, $fd, $reactor_id, $data) { /** @var Request $request */ $request = Request::create(); $request->parse($data); /** @var Response $response */ $response = Response::create([$server, $request], $fd); $response->status(200); $response->end('Hello, Swoole'); }); ``` 不但异步 Server 中可以使用,而且协程 Server 中也可以使用: ```php use Swoole\Coroutine\Server; use Swoole\Coroutine\Server\Connection; use Swoole\Http\Request; use Swoole\Http\Response; use function Swoole\Coroutine\run; run(function () { $server = new Server('0.0.0.0', 9501, false); $server->handle(function (Connection $conn) use ($server) { $request = Request::create(); while(true) { $data = $conn->recv(); if (strlen($data) != $request->parse($data) || $request->isCompleted()) { break; } } $response = Response::create([$conn->exportSocket(), $request]); $response->end('Hello, Swoole'); }); $server->start(); }); ``` 这样就可以直接使用 `Http\Response` 的 API 进行响应处理,而不用自行组装 HTTP 协议响应数据。 ## isWritable `isWritable()` 用来判断 `Http\Response` 对象是否已结束 (`end`) 或已分离 (`detach`),例如: ```php use Swoole\Http\Server; use Swoole\Http\Request; use Swoole\Http\Response; $http = new Server('0.0.0.0', 9501); $http->on('request', function (Request $req, Response $resp) { assert($resp->isWritable(), true); $resp->end('hello'); assert($resp->isWritable(), false); $resp->setStatusCode(403); }); $http->start(); ``` 如果在调用`end()`之后,再次调用相关 API,就会发生错误 ```bash PHP Warning: Swoole\Http\Response::setStatusCode(): http response is unavailable (maybe it has been ended or detached) ``` ![](https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/swoole/wechat.png)
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
还没有评论!
微信公众号
热门内容
作者其它话题
- thinkphp5.1在使用think-swoole的时候报错unsupported option [host]
- WSL 下服务器响应数据过大无法接收
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。