For PHP FPM, each piece of code was executed individually, and the result (output of that process) was piped back to a client. So whenever we wanted to stop processing, we just triggered exit()
.
With Swoole, however, the server is expected to run all the time. Sure, it's possible to stop the Process with Swoole\Process::exit() - but it's usually up to controller's to trigger sending the response immediately. For example:
$server = new Swoole\HTTP\Server("127.0.0.1", 9501);
$server->on("start", function (Swoole\Http\Server $server) {
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});
$server->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});
$server->start();
In this case, $response->end
is the method that does essentially the same thing as exit()
in PHP FPM world. Note that it's quite similar to what happens in Node world: the server is expected to be running all the time, and it's the controllers (functions processing each individual request) deciding whether to stop handling individual request and pass back the response, both headers and body.