Swoole server

Swoole server

Swoole server provides the API for developers to write TCP, UDP, Unix Socket asynchronous servers. It supports IPv4, IPv6, one Way, two Way SSL and TLS Encryption. Developers do not have to know the internal implementations, only have to write the logics of the server in the callback functions.

Swoole server apis only can be used in the php cli mode.


Initialize the server

$server = new swoole_server("", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);

Set runtime paramaters

    'worker_num' => 4, // The number of worker processes
    'daemonize' => true, // Whether start as a daemon process
    'backlog' => 128, // TCP backlog connection number

Register callback functions

$server->on('Connect', 'my_onConnect');
$server->on('Receive', 'my_onReceive');
$server->on('Close', 'my_onClose');

There are four types of callback functions

Start the server


Get the server attributes

$server->manager_pid; // PID of manager process, send SIGUSR1 to this process to reload the application
$server->master_pid;  // PID of master process, send SIGTERM signal to this process to shutdown the server
$server->connections; // The connections established