This commit is contained in:
freek 2018-12-03 14:24:06 +01:00
parent ce9da0d0f0
commit 61fe3621be
4 changed files with 31 additions and 14 deletions

View File

@ -25,14 +25,15 @@
"php": "^7.1", "php": "^7.1",
"ext-json": "*", "ext-json": "*",
"cboden/ratchet": "^0.4.1", "cboden/ratchet": "^0.4.1",
"illuminate/broadcasting": "5.7.*",
"illuminate/console": "5.7.*", "illuminate/console": "5.7.*",
"illuminate/http": "5.7.*", "illuminate/http": "5.7.*",
"illuminate/routing": "5.7.*", "illuminate/routing": "5.7.*",
"illuminate/broadcasting": "5.7.*",
"illuminate/support": "5.7.*", "illuminate/support": "5.7.*",
"symfony/http-kernel": "~4.0",
"pusher/pusher-php-server": "~3.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": { "require-dev": {
"mockery/mockery": "^1.2", "mockery/mockery": "^1.2",

View File

@ -74,8 +74,18 @@ class StartWebSocketServer extends Command
protected function configureStatisticsLogger() 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() { $this->loop->addPeriodicTimer(60, function() {
StatisticsLogger::save(); StatisticsLogger::save($this->loop);
}); });
return $this; return $this;

View File

@ -2,7 +2,9 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Logging; namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Client;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
@ -14,9 +16,11 @@ class StatisticsLogger
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager; protected $channelManager;
public function __construct(ChannelManager $channelManager) public function __construct(ChannelManager $channelManager, Client $client)
{ {
$this->channelManager = $channelManager; $this->channelManager = $channelManager;
$this->client = $client;
} }
public function webSocketMessage(ConnectionInterface $connection) public function webSocketMessage(ConnectionInterface $connection)
@ -57,14 +61,20 @@ class StatisticsLogger
public function save() public function save()
{ {
foreach ($this->statistics as $appId => $statistic) { foreach ($this->statistics as $appId => $statistic) {
if ($statistic->isEnabled()) { if (! $statistic->isEnabled()) {
// TODO: perform http request continue;
} }
$this->client->postAsync(
action([WebsocketStatisticsEntriesController::class, 'store']),
$statistic->toArray()
);
// Reset connection and message count // Reset connection and message count
$connections = Collection::make($this->channelManager->getChannels($appId))->sum(function ($channel) { $connections = collect($this->channelManager->getChannels($appId))
return count($channel->getSubscribedConnections()); ->sum(function ($channel) {
}); return count($channel->getSubscribedConnections());
});
$statistic->reset($connections); $statistic->reset($connections);
} }

View File

@ -52,10 +52,6 @@ class WebSocketsServiceProvider extends ServiceProvider
return new ChannelManager(); return new ChannelManager();
}); });
$this->app->singleton('websockets.statisticslogger', function() {
return new StatisticsLogger(app(ChannelManager::class));
});
$this->app->singleton(AppProvider::class, function() { $this->app->singleton(AppProvider::class, function() {
return app(config('websockets.app_provider')); return app(config('websockets.app_provider'));
}); });