Swoole\Http\Response->detach

Declaration

<?php
Swoole\Http\Response->detach()

Parameters

Return

Response

Description

Detach the http response object with current context. Then you can construct and send back the response within another context such as another Task Worker.

Example

<?php
$server = new Swoole\Http\Server("0.0.0.0", 9501);

$server->set(['task_worker_num' => 1, 'worker_num' => 1]);

$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server) {
    $response->detach();
    $server->task(strval($response->fd));
});

$server->on('finish', function () {
    echo "task finish";
});

$server->on('task', function ($serv, $task_id, $worker_id, $data) {
    $resp = Swoole\Http\Response::create($data);
    $resp->end("task_worker_content");
    echo "async task\n";
});

$server->start();
<?php
$server = new Swoole\Http\Server("0.0.0.0", 9501);

$server->on('request', function ($req, Swoole\Http\Response $response) use ($server) {
    $response->detach();
    $server->send($response->fd, "HTTP/1.1 200 OK\r\nServer: SWOOLE\r\n\r\nHello World\n");
});

$server->start();