Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Http\Response::create(int $fd): OpenSwoole\Http\Response
The client connection $fd number
If successful, a newly created OpenSwoole response object is returned otherwise false
Construct a new OpenSwoole\Http\Response
object from a client connection $fd
. This method is usually used for when you are detaching a response.
You use this method to reconstruct a OpenSwoole HTTP response object after it was detached, allowing you to use it as normal, kind of reattaching the response again after it has been passed around to a Task Worker for example.
Make sure the response object has been detached before calling this method otherwise the server may send back the same request twice.
<?php
$server = new OpenSwoole\Http\Server("127.0.0.1", 9501);
$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response)
{
$response->detach();
$response2 = OpenSwoole\Http\Response::create($response->fd);
$response2->end("Hello World!");
});
// Other event methods...
$server->start();
Or you could temporarily detach to use the $server
methods:
<?php
$server = new OpenSwoole\Http\Server("127.0.0.1", 9501);
$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response) use ($server)
{
$response->detach();
$server->...($response->fd);
$response2 = OpenSwoole\Http\Response::create($response->fd);
$response2->end("Hello World!");
});
// Other event methods...
$server->start();