## Http\\Response->detach
分离响应对象。使用此方法后,$response对象销毁时不会自动end,与Http\Response::create和Server::send配合使用。
~~~
function Http\Response->detach();
~~~
* 客户端已完成响应,操作失败返回`false`,成功返回`true`
<br>
## 跨进程响应
某些情况下,需要在Task进程中对客户端发出响应。这时可以利用detach使$response对象独立。在Task进程可以重新构建$response,发起Http请求响应。
~~~
$http = new swoole_http_server("0.0.0.0", 9501);
$http->set(['task_worker_num' => 1, 'worker_num' => 1]);
$http->on('request', function ($req, Swoole\Http\Response $resp) use ($http) {
$resp->detach();
$http->task(strval($resp->fd));
});
$http->on('finish', function ()
{
echo "task finish";
});
$http->on('task', function ($serv, $task_id, $worker_id, $data)
{
var_dump($data);
$resp = Swoole\Http\Response::create($data);
$resp->end("in task");
echo "async task\n";
});
$http->start();
~~~
<br>
## 发送任意内容
某些特殊的场景下,需要对客户端发送特殊的响应内容。`Http\Response`对象自带的`end`方法无法满足需求,可以使用`detach`分离响应对象,然后自行组包并使用`Server::send`发送数据。
~~~
$http = new swoole_http_server("0.0.0.0", 9501);
$http->on('request', function ($req, Swoole\Http\Response $resp) use ($http) {
$resp->detach();
$http->send($resp->fd, "HTTP/1.1 200 OK\r\nServer: server\r\n\r\nHello World\n");
});
$http->start();
~~~
- 序章
- 1.环境搭建
- PHP7源码编译安装
- Swoole源码编译安装
- Mysql5.7源码安装
- Redis安装
- 2.搭建Echo服务器
- 3.Server服务器
- 函数列表
- Server::__construct
- Server->set
- Server->on
- Server->start
- Server->send
- WebSocket
- Server->push
- Server->exist
- Server::pack
- Server::unpack
- Server->disconnect
- Server->isEstablished
- 配置选项
- reactor_num
- worker_num
- max_request
- max_conn
- daemonize
- backlog
- log_file
- log_level
- upload_tmp_dir
- http_parse_post
- document_root
- http_compression
- 事件回调函数
- onStart
- onWorkerStart
- onConnect
- onReceive
- onPacket
- onRequest
- 请求Request
- Http\Request->$header
- Http\Request->$server
- Http\Request->$get
- Http\Request->$post
- Http\Request->$cookie
- Http\Request->$files
- Http\Request->rawContent
- Http\Request->getData
- 响应Response
- Http\Response->header
- Http\Response->cookie
- Http\Response->status
- Http\Response->redirect
- Http\Response->write
- Http\Response->sendfile
- Http\Response->end
- Http\Response->detach
- Http\Response::create
- onClose
- onOpen
- onMessage
- 创建服务器
- TCP服务器
- UDP服务器
- HTTP服务器
- WebSocket服务器
- 4.定时器Timer
- 5.进程Process
- Process::__construct
- Process->start
- Process->name
- Process->exec
- Process->write
- Process->read
- Process->setTimeout
- Process->setBlocking
- Process->useQueue
- Process->statQueue
- Process->freeQueue
- Process->push
- Process->pop
- Process->close
- Process->exit
- Process::kill
- Process::wait
- Process::daemon
- Process::signal
- 6.内存Memory
- Table
- Table->__construct
- Table->column
- Table->create
- Table->set
- Table->incr
- Table->decr
- Table->get
- Table->exist
- Table->count
- Table->del
- Channel
- Channel->__construct
- Channel->push
- Channel->pop
- Channel->stats
- 7.协程Coroutine
- Coroutine
- Coroutine::list
- Coroutine::set
- Coroutine::stats
- Coroutine::create
- Coroutine::exist
- Coroutine::getCid
- Coroutine::getContext
- Coroutine::defer
- Coroutine::getBackTrace
- Coroutine::yield
- Coroutine::resume
- Coroutine::fread
- Coroutine::fgets
- Coroutine::fwrite
- Coroutine::sleep
- Coroutine::gethostbyname
- Coroutine::getaddrinfo
- Coroutine::exec
- Coroutine::readFile
- Coroutine::writeFile
- Coroutine::statvfs
- Coroutine::getPcid
- Coroutine\Channel
- Coroutine\Channel->__construct
- Coroutine\Channel->push
- Coroutine\Channel->pop
- Coroutine\Channel->stats
- Coroutine\Channel->close
- Coroutine\Channel->length
- Coroutine\Channel->isEmpty
- Coroutine\Channel->isFull
- Coroutine\Channel->$capacity
- Coroutine\Channel->$errCode
- Coroutine\Client
- Coroutine\Client->connect
- Coroutine\Client->send
- Coroutine\Client->recv
- Coroutine\Client->close
- Coroutine\Client->peek
- Coroutine\Client->set
- Coroutine\Http\Client
- Coroutine\Http\Client->get
- Coroutine\Http\Client->post
- 其他
- 并行和并发的区别
- 堆、栈、队列