<?php // Create and start timer firing after 2 seconds $w1 = new EvTimer(2, 0, function () { echo "2 seconds elapsed\n"; }); // Create and launch timer firing after 2 seconds repeating each second // until we manually stop it $w2 = new EvTimer(2, 1, function ($w) { echo "is called every second, is launched after 2 seconds\n"; echo "iteration = ", Ev::iteration(), PHP_EOL; // Stop the watcher after 5 iterations Ev::iteration() == 5 and $w->stop(); // Stop the watcher if further calls cause more than 10 iterations Ev::iteration() >= 10 and $w->stop(); }); // Create stopped timer. It will be inactive until we start it ourselves $w_stopped = EvTimer::createStopped(10, 5, function ($w) { echo "Callback of a timer created as stopped\n"; // Stop the watcher after 2 iterations Ev::iteration() >= 2 and $w->stop(); }); // Loop until Ev::stop() is called or all of watchers stop Ev::run(); // Start and look if it works $w_stopped->start(); echo "Run single iteration\n"; Ev::run(Ev::RUN_ONCE); echo "Restart the second watcher and try to handle the same events, but don't block\n"; $w2->again(); Ev::run(Ev::RUN_NOWAIT);
<?php /** * Create and bind socket */ $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); socket_bind($socket, '127.0.0.1', 8081) or die("Could not bind to socket\n"); socket_listen($socket, 3) or die("Could not set up socket listener\n"); /** * IO timeout watcher */ $timeout_watcher = new EvTimer(10.0, 0.0, function () use($socket) { socket_close($socket); Ev::stop(Ev::BREAK_ALL); }); /** * Load a new Scale Application */ $container = (require 'bootstrap.php'); $app = $container->constructInject('\\Scale\\Kernel\\Core\\Webapp'); $executor = $app->getExecutor(); $container->inform($executor); /** * Monitor socket read events in loop */ $read_watcher = new EvIo($socket, Ev::READ, function ($w, $re) use($socket, $app, $executor, $timeout_watcher) { // Stop timeout watcher $timeout_watcher->stop(); // Stop write watcher $w->stop(); // Connect and read from the client