/** * @param Topic $topic * * @return array */ public function registerPeriodicTimer(Topic $topic) { //add $this->periodicTimer->addPeriodicTimer($this, 'hello', 5, function () use($topic) { $topic->broadcast(['msg' => "ellooooo"]); }); //exist $this->periodicTimer->isPeriodicTimerActive($this, 'hello'); // true or false //remove $this->periodicTimer->cancelPeriodicTimer($this, 'hello'); }
/** * @param string $calledMethod * @param ConnectionInterface $conn * @param Topic $topic * @param null $payload * @param null $exclude * @param null $eligible * * @return bool */ public function dispatch($calledMethod, ConnectionInterface $conn, Topic $topic, WampRequest $request, $payload = null, $exclude = null, $eligible = null) { $dispatched = false; foreach ((array) $request->getRoute()->getCallback() as $callback) { $appTopic = $this->topicRegistry->getTopic($callback); if ($topic) { if ($appTopic instanceof TopicPeriodicTimerInterface) { $appTopic->setPeriodicTimer($this->topicPeriodicTimer); if (false === $this->topicPeriodicTimer->isRegistered($appTopic) && 0 !== count($topic)) { $appTopic->registerPeriodicTimer($topic); } } if ($calledMethod === static::UNSUBSCRIPTION && 0 === count($topic)) { $this->topicPeriodicTimer->clearPeriodicTimer($appTopic); } try { if ($payload) { //its a publish call. $appTopic->{$calledMethod}($conn, $topic, $request, $payload, $exclude, $eligible); } else { $appTopic->{$calledMethod}($conn, $topic, $request); } } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['code' => $e->getCode(), 'file' => $e->getFile(), 'trace' => $e->getTraceAsString()]); $conn->callError($topic->getId(), $topic, $e->getMessage(), ['topic' => $topic, 'request' => $request, 'event' => $calledMethod]); return; } $dispatched = true; } } return $dispatched; }