Adds a recurring callback to execute on a specified interval. Proxies to
addPeriodTimer() implementation of the event loop implementation
returned by getLoop().
public addPeriodicTimer ( numeric $interval, callable $callback ) : React\Event\Timer\TimerInterface | ||
$interval | numeric | Number of seconds to wait between executions of callback |
$callback | callable | Callback to execute |
return | React\Event\Timer\TimerInterface | Added timer |
/** * Tests addPeriodicTimer(). */ public function testAddPeriodicTimer() { $interval = 5; $callback = function () { }; $timer = $this->getMockTimer(); $loop = $this->getMockLoop(); Phake::when($loop)->addPeriodicTimer($interval, $callback)->thenReturn($timer); Phake::when($this->client)->getLoop()->thenReturn($loop); $this->assertSame($timer, $this->client->addPeriodicTimer($interval, $callback)); }
/** * Reads from the XMPP Socket and writes to the IRC channel */ private function attachSocketReadTimer() { /** * Read messages from the socket and write them to the channel * * @noinspection PhpParamsInspection */ $this->client->addPeriodicTimer(1, function () { if (!$this->connected) { return; } $text = $this->xmppStream->read(); if (!empty($text)) { $messageParts = explode(' ', $text); $nickName = str_replace(['<', '>'], '', $messageParts[0]); if ($nickName != $this->getContainer()->get('config')['xmpp']['nickname'] && $nickName != $this->getContainer()->get('config')['irc']['nickname']) { $this->getContainer()->get('logger')->debug('Found a message in the XMPP socket. Writing it to the IRC channel'); $this->write->ircPrivmsg($this->getContainer()->get('config')['irc']['channel'], htmlspecialchars_decode($text)); } } }); }