Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Server->shutdown(): void
none
If success, it returns a true
otherwise it returns false
.
Shutdown the master server process and turn off the server, this function can be called in worker processes.
It is possible to shutdown a server by sending the system signal SIGTERM:
kill -15 MASTER_PID
<?php
$server = new Swoole\Server("127.0.0.1", 9501);
$server->on('connect', function ($server, $fd)
{
echo "New connection established: #{$fd}.\n";
});
$server->on('receive', function ($server, $fd, $from_id, $data)
{
if(trim($data) == "shutdown")
{
// Turn off the server
$server->shutdown();
}
else
{
$server->send($fd, "Echo to #{$fd}: \n".$data);
$server->close($fd);
}
});
// Shutdown event is triggered
$server->on('Shutdown', function($server, $worker_id)
{
echo "Shutting down server...";
});
$server->on('WorkerStop', function($server, $worker_id)
{
echo $worker_id . " stop\n";
});
$server->on('close', function ($server, $fd)
{
echo "Connection closed: #{$fd}.\n";
});
$server->start();