$base = new EventBase(); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($socket, '127.0.0.1', 8000); socket_listen($socket); $event = new Event($base, $socket, Event::READ | Event::PERSIST, function($socket, $flags, $base){ $client = socket_accept($socket); $data = socket_read($client, 1024); echo $data; }); $event->add(); $base->loop();
$base = new EventBase(); $event = new Event($base, SIGINT, Event::SIGNAL, function($signal, $flags, $base){ echo "Received signal {$signal}\n"; $base->stop(); }); $event->add(); $base->loop();This code creates a new EventBase and an Event to handle the SIGINT signal. When the signal is received, the anonymous function inside the Event is called, which prints a message and stops the EventBase loop. Package library: pecl/libevent. Overall, PHP Event is a powerful library for handling asynchronous events in PHP applications. With its support for I/O events, signals, and timers, it is a useful tool for building high-performance, scalable applications.