Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Server->on(string $event , callable $callback)
The event name
callback function
if success, it returns TRUE, otherwise it returns FALSE.
Register callback function for a server event.
OpenSwoole\Server->on('start', fn)
OpenSwoole\Server->on('shutdown', fn)
OpenSwoole\Server->on('workerstart', fn)
OpenSwoole\Server->on('workerstop', fn)
OpenSwoole\Server->on('timer', fn)
OpenSwoole\Server->on('connect', fn)
OpenSwoole\Server->on('receive', fn)
OpenSwoole\Server->on('packet', fn)
OpenSwoole\Server->on('close', fn)
OpenSwoole\Server->on('task', fn)
OpenSwoole\Server->on('finish', fn)
OpenSwoole\Server->on('pipemessage', fn)
OpenSwoole\Server->on('workererror', fn)
OpenSwoole\Server->on('managerstart', fn)
OpenSwoole\Server->on('managerstop', fn)
OpenSwoole\Server->on('beforereload', fn)
OpenSwoole\Server->on('afterreload', fn)
<?php
$server = new OpenSwoole\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();
Repeated calls to the same event will override any previously set callbacks