TCP/UDP Server provides the API 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.
Notice: Server API only can be used in the php CLI mode.
Swoole\Server::__construct
Swoole\Server->set
Swoole\Server->on
Swoole\Server->listen
Swoole\Server->addListener
Swoole\Server->addProcess
Swoole\Server->addTimer
Swoole\Server->start
Swoole\Server->reload
Swoole\Server->stop
Swoole\Server->shutdown
Swoole\Server->tick
Swoole\Server->after
Swoole\Server->defer
Swoole\Server->clearTimer
Swoole\Server->close
Swoole\Server->send
Swoole\Server->sendfile
Swoole\Server->sendto
Swoole\Server->sendwait
Swoole\Server->sendMessage
Swoole\Server->exist
Swoole\Server->pause
Swoole\Server->resume
Swoole\Server->getClientInfo
Swoole\Server->getClientList
Swoole\Server->bind
Swoole\Server->stats
Swoole\Server->task
Swoole\Server->taskCo
Swoole\Server->taskWait
Swoole\Server->taskWaitMulti
Swoole\Server->finish
Swoole\Server->heartbeat
Swoole\Server->getLastError
Swoole\Server->getSocket
Swoole\Server->protect
Swoole\Server->getCallback
Swoole\Server->getReceivedTime
Swoole\Server->getWorkerId
Swoole\Server->getWorkerPid
Swoole\Server->getManagerPid
Swoole\Server->getMasterPid
Swoole\Server->on('start', fn)
Swoole\Server->on('shutdown', fn)
Swoole\Server->on('workerstart', fn)
Swoole\Server->on('workerstop', fn)
Swoole\Server->on('timer', fn)
Swoole\Server->on('connect', fn)
Swoole\Server->on('receive', fn)
Swoole\Server->on('packet', fn)
Swoole\Server->on('close', fn)
Swoole\Server->on('task', fn)
Swoole\Server->on('finish', fn)
Swoole\Server->on('pipemessage', fn)
Swoole\Server->on('workererror', fn)
Swoole\Server->on('managerstart', fn)
Swoole\Server->on('managerstop', fn)
Swoole\Server->on('beforereload', fn)
Swoole\Server->on('afterreload', fn)
<?php
$server = new Swoole\Server("127.0.0.1", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);
<?php
$server->set(array(
'worker_num' => 4, // The number of worker processes
'daemonize' => true, // Whether start as a daemon process
'backlog' => 128, // TCP backlog connection number
));
<?php
$server->on('connect', function() {
});
$server->on('receive', function() {
});
$server->on('close', function() {
});
There are four types of callback functions
<?php
$server->start();
<?php
$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
The swoole doesn't support the set_exception_handler
function.
If there is the logic of throwing exception in your code, it must add the try/catch
in the very beginning of callback function.
<?php
$server->on('Timer', function() {
try {
//some code
} catch(Exception $e) {
//exception code
}
}
MIME helper functions:
Since version 4.5.0
function swoole_mime_type_add(string $suffix, string $mime_type){}
function swoole_mime_type_set(string $suffix, string $mime_type){}
function swoole_mime_type_delete(string $suffix, string $mime_type){}
function swoole_mime_type_get(string $filename){}
function swoole_mime_type_exists(string $filename){}
void Swoole\Server->getWorkerStatus (): SWOOLE_WORKER_BUSY|SWOOLE_WORKER_IDLE
Since version 4.5.0
Get the status of current worker process.
void Swoole\Server->get(Master|Manager|Worker)Pid (): int
Since version 4.5.0
Get the Pid of Master/Manager/Worker process.
void Swoole\Server->getWorkerId (): int
Since version 4.5.0
Get the Worker ID.