diff --git a/composer.json b/composer.json index b3394e1..54fcc77 100644 --- a/composer.json +++ b/composer.json @@ -25,14 +25,15 @@ "php": "^7.1", "ext-json": "*", "cboden/ratchet": "^0.4.1", + "illuminate/broadcasting": "5.7.*", "illuminate/console": "5.7.*", "illuminate/http": "5.7.*", "illuminate/routing": "5.7.*", - "illuminate/broadcasting": "5.7.*", "illuminate/support": "5.7.*", - "symfony/http-kernel": "~4.0", "pusher/pusher-php-server": "~3.0", - "symfony/psr-http-message-bridge": "^1.1" + "symfony/http-kernel": "~4.0", + "symfony/psr-http-message-bridge": "^1.1", + "wyrihaximus/react-guzzle-psr7": "^2.1" }, "require-dev": { "mockery/mockery": "^1.2", diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 533fc09..0f283f2 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -74,8 +74,18 @@ class StartWebSocketServer extends Command protected function configureStatisticsLogger() { + $handler = new \WyriHaximus\React\GuzzlePsr7\HttpClientAdapter($this->loop); + + $client = new \GuzzleHttp\Client([ + 'handler' => \GuzzleHttp\HandlerStack::create($handler), + ]); + + app()->singleton('websockets.statisticslogger', function() use ($client) { + return new StatisticsLogger(app(ChannelManager::class, $client)); + }); + $this->loop->addPeriodicTimer(60, function() { - StatisticsLogger::save(); + StatisticsLogger::save($this->loop); }); return $this; diff --git a/src/Statistics/Logging/StatisticsLogger.php b/src/Statistics/Logging/StatisticsLogger.php index 2dac99e..82cd791 100644 --- a/src/Statistics/Logging/StatisticsLogger.php +++ b/src/Statistics/Logging/StatisticsLogger.php @@ -2,7 +2,9 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logging; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; +use GuzzleHttp\Client; use Illuminate\Support\Collection; use Ratchet\ConnectionInterface; @@ -14,9 +16,11 @@ class StatisticsLogger /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ protected $channelManager; - public function __construct(ChannelManager $channelManager) + public function __construct(ChannelManager $channelManager, Client $client) { $this->channelManager = $channelManager; + + $this->client = $client; } public function webSocketMessage(ConnectionInterface $connection) @@ -57,14 +61,20 @@ class StatisticsLogger public function save() { foreach ($this->statistics as $appId => $statistic) { - if ($statistic->isEnabled()) { - // TODO: perform http request + if (! $statistic->isEnabled()) { + continue; } + $this->client->postAsync( + action([WebsocketStatisticsEntriesController::class, 'store']), + $statistic->toArray() + ); + // Reset connection and message count - $connections = Collection::make($this->channelManager->getChannels($appId))->sum(function ($channel) { - return count($channel->getSubscribedConnections()); - }); + $connections = collect($this->channelManager->getChannels($appId)) + ->sum(function ($channel) { + return count($channel->getSubscribedConnections()); + }); $statistic->reset($connections); } diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 6b9c97f..06190ed 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -52,10 +52,6 @@ class WebSocketsServiceProvider extends ServiceProvider return new ChannelManager(); }); - $this->app->singleton('websockets.statisticslogger', function() { - return new StatisticsLogger(app(ChannelManager::class)); - }); - $this->app->singleton(AppProvider::class, function() { return app(config('websockets.app_provider')); });