channelManager = $channelManager; $this->browser = $browser; } /** * Handle the incoming websocket message. * * @param \Ratchet\ConnectionInterface $connection * @return void */ public function webSocketMessage(ConnectionInterface $connection) { $this->findOrMakeStatisticForAppId($connection->app->id) ->webSocketMessage(); } /** * Handle the incoming API message. * * @param mixed $appId * @return void */ public function apiMessage($appId) { $this->findOrMakeStatisticForAppId($appId) ->apiMessage(); } /** * Handle the new conection. * * @param \Ratchet\ConnectionInterface $connection * @return void */ public function connection(ConnectionInterface $connection) { $this->findOrMakeStatisticForAppId($connection->app->id) ->connection(); } /** * Handle disconnections. * * @param \Ratchet\ConnectionInterface $connection * @return void */ public function disconnection(ConnectionInterface $connection) { $this->findOrMakeStatisticForAppId($connection->app->id) ->disconnection(); } /** * Save all the stored statistics. * * @return void */ public function save() { foreach ($this->statistics as $appId => $statistic) { if (! $statistic->isEnabled()) { continue; } $postData = array_merge($statistic->toArray(), [ 'secret' => App::findById($appId)->secret, ]); $this->browser ->post( action([WebSocketStatisticsEntriesController::class, 'store']), ['Content-Type' => 'application/json'], stream_for(json_encode($postData)) ); $currentConnectionCount = $this->channelManager->getConnectionCount($appId); $statistic->reset($currentConnectionCount); } $this->statistics = []; } /** * Find or create a defined statistic for an app. * * @param mixed $appId * @return Statistic */ protected function findOrMakeStatisticForAppId($appId): Statistic { if (! isset($this->statistics[$appId])) { $this->statistics[$appId] = new Statistic($appId); } return $this->statistics[$appId]; } }