1

I was wondering, how can I shutdown a reactPHP HTTP server after a sending a response.

Currently I have a server code like this:

$loop = Factory::create();
$server = new Server($loop, function (ServerRequestInterface $request) {
    var_dump($request);
    return new Response(200,
        array(
            'Content-Type' => 'text/plain'
        ),
        "I got ".$request->getBody()."\n");
});
$socket = new \React\Socket\Server(8070, $loop);
$server->listen($socket);

$loop->run();

And a client who perform a single request:

$loop = React\EventLoop\Factory::create();
$client = new React\Http\Browser($loop);


$client->post(
    'http://localhost:8070',
    array(
        'Content-Type' => 'application/json'
    ),
    json_encode($_GET)
)->then(function (ResponseInterface $response) {
    echo (string)$response->getBody();
}, 'printf');

$loop->run();

Is there any event which I can use to shutdown server after responding. I don't want to use exit(), because after shutdown I want to perform other tasks before true exit.

Eddy
  • 593
  • 8
  • 22

1 Answers1

1

ReactPHP core team dev here, the easiest way you can do that is to call close on $socket. When you do that the socket will stop listening for incoming connections and as a result, the event loop has nothing more to do and will stop.

WyriHaximus
  • 1,000
  • 6
  • 8
  • I have solved it by sending second request, which will be resolved as 'shutdown-call' on the server. The second request is sent just after getting response from first request. – Eddy Oct 28 '20 at 16:02