protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<info>Lykov bot started</info>'); $loop = \React\EventLoop\Factory::create(); $emitter = new \Evenement\EventEmitter(); $pcntl = new \MKraemer\ReactPCNTL\PCNTL($loop); $pcntl->on(SIGTERM, function () use($emitter) { $emitter->emit('terminate'); }); $pcntl->on(SIGTERM, function () use($emitter) { $emitter->emit('terminate'); }); $emitter->on('error.fatal', function ($msg) use($loop, $output) { $loop->stop(); $output->writeln('<fg="red">Fatal error occured: ' . $msg . '</fg>'); }); $emitter->on('terminate', function () use($loop, $emitter) { $emitter->emit('log', ['Terminating...', OutputInterface::VERBOSITY_NORMAL]); $loop->stop(); die; }); $emitter->on('log', function ($msg, $verbosity = OutputInterface::VERBOSITY_DEBUG) use($output) { if ($verbosity === OutputInterface::VERBOSITY_DEBUG) { $msg = 'DEBUG: ' . $msg; } if ($output->getVerbosity() >= $verbosity) { $output->writeln($msg); } }); $apiClient = new SocketClient($loop, $emitter, self::TOKEN_LYKOV_TEST); $emitter->emit('send', ['id' => 2, 'type' => 'message', 'channel' => 'D04F44TFL', 'text' => 'hello']); $loop->run(); }
* Benchmarking against EventEmitter (original by Igor Wiedler) */ $loader = (require __DIR__ . '/../vendor/autoload.php'); require 'diff.php'; $start = millitime(); $emitter = new \Evenement\EventEmitter(); $counter = 1; $listeners = 100; $emitters = 10000; for ($i = 1; $i <= $listeners; $i++) { $emitter->on('listener_' . $i, function () use(&$counter) { $counter++; }); } for ($i = 1; $i <= $emitters; $i++) { $emitter->emit('listener_' . $i); } $time_EventEmitter = millitimeDiff($start); $start = millitime(); $emitter = new \Evenement\EventEmitterRegex(); $counter = 1; for ($i = 1; $i <= $listeners; $i++) { $emitter->on('listener_' . $i, function () use(&$counter) { $counter++; }); } for ($i = 1; $i <= $emitters; $i++) { $emitter->emit('listener_' . $i . '$'); } $time_EventEmitterRegex = millitimeDiff($start); printf("Time for {$listeners} listeners and {$emitters} emits\n" . "EventEmitter: \t\t\t %-5d ms\n" . "EventEmitterRegex:\t\t\t %-5d ms\n", $time_EventEmitter, $time_EventEmitterRegex);