<?php
Swoole\Server->on ( string $event , callable $callback )
The event name
callback function
if success, it returns TRUE, otherwise it returns FALSE.
Register callback function for the event.
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);
$server->on('connect', function ($server, $fd){
echo "New connection established: #{$fd}.\n";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
$server->send($fd, "Echo to #{$fd}: \n".$data);
$server->close($fd);
});
$server->on('close', function ($server, $fd) {
echo "Connection closed: #{$fd}.\n";
});
$server->start();