Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit dd283c6

Browse files
committed
Merge branch 'master' of github.com:beyondcode/laravel-websockets
2 parents 1f0a153 + e738d30 commit dd283c6

File tree

7 files changed

+79
-58
lines changed

7 files changed

+79
-58
lines changed

config/websockets.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@
4242
*/
4343
'max_request_size_in_kb' => 250,
4444

45-
/*
46-
* This model will be used to store the statistics of the WebSocketsServer
47-
*/
48-
'statistics_model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,
45+
'statistics' => [
46+
/*
47+
* This model will be used to store the statistics of the WebSocketsServer.
48+
* The only requirement is that the model should be or extend
49+
* `WebSocketsStatisticsEntry` provided by this package.
50+
*/
51+
'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,
52+
53+
/*
54+
* Here you can specify the interval in seconds at which statistics should be logged.
55+
*/
56+
'interval_in_seconds' => 60,
57+
],
4958

5059
/*
5160
* Define the optional SSL context for your WebSocket connections.

src/Console/StartWebSocketServer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ protected function configureStatisticsLogger()
5353

5454
$browser = new Browser($this->loop, $connector);
5555

56-
5756
app()->singleton(StatisticsLoggerInterface::class, function() use ($browser) {
5857
return new HttpStatisticsLogger(app(ChannelManager::class), $browser);
5958
});
6059

61-
$this->loop->addPeriodicTimer(5, function() {
60+
$this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function() {
6261
StatisticsLogger::save();
6362
});
6463

src/Statistics/Events/StatisticsUpdated.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BeyondCode\LaravelWebsockets\Statistics\Events;
44

55
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
6+
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
67
use Illuminate\Broadcasting\PrivateChannel;
78
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
89
use Illuminate\Queue\SerializesModels;
@@ -11,27 +12,30 @@ class StatisticsUpdated implements ShouldBroadcast
1112
{
1213
use SerializesModels;
1314

14-
public $statisticModel;
15+
/** @var \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry */
16+
protected $webSocketsStatisticsEntry;
1517

16-
public function __construct($statisticModel)
18+
public function __construct(WebSocketsStatisticsEntry $webSocketsStatisticsEntry)
1719
{
18-
$this->statisticModel = $statisticModel;
20+
$this->webSocketsStatisticsEntry = $webSocketsStatisticsEntry;
1921
}
2022

2123
public function broadcastWith()
2224
{
2325
return [
24-
'time' => (string)$this->statisticModel->created_at,
25-
'app_id' => $this->statisticModel->app_id,
26-
'peak_connection_count' => $this->statisticModel->peak_connection_count,
27-
'websocket_message_count' => $this->statisticModel->websocket_message_count,
28-
'api_message_count' => $this->statisticModel->api_message_count,
26+
'time' => $this->webSocketsStatisticsEntry->created_at->timestamp,
27+
'app_id' => $this->webSocketsStatisticsEntry->appId,
28+
'peak_connection_count' => $this->webSocketsStatisticsEntry->peakConnectionCount,
29+
'websocket_message_count' => $this->webSocketsStatisticsEntry->webSocketMessageCount,
30+
'api_message_count' => $this->webSocketsStatisticsEntry->apiMessageCount,
2931
];
3032
}
3133

3234
public function broadcastOn()
3335
{
34-
return new PrivateChannel(str_after(DashboardLogger::LOG_CHANNEL_PREFIX . 'statistics', 'private-'));
36+
$channelName = str_after(DashboardLogger::LOG_CHANNEL_PREFIX . 'statistics', 'private-');
37+
38+
return new PrivateChannel($channelName);
3539
}
3640

3741
public function broadcastAs()

src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function store(Request $request)
1717
'api_message_count' => 'required|integer',
1818
]);
1919

20-
$webSocketsStatisticsEntryModelClass = config('websockets.statistics_model');
20+
$webSocketsStatisticsEntryModelClass = config('websockets.statistics.model');
2121

2222
$statisticModel = $webSocketsStatisticsEntryModelClass::create($validatedAttributes);
2323

src/Statistics/Logger/HttpStatisticsLogger.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
class HttpStatisticsLogger implements StatisticsLogger
1313
{
14-
/** @var Statistic[] */
14+
/** @var \BeyondCode\LaravelWebSockets\Statistics\Statistic[] */
1515
protected $statistics = [];
1616

1717
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
1818
protected $channelManager;
1919

20-
/** @var Browser */
20+
/** @var \Clue\React\Buzz\Browser */
2121
protected $browser;
2222

2323
public function __construct(ChannelManager $channelManager, Browser $browser)
@@ -29,37 +29,39 @@ public function __construct(ChannelManager $channelManager, Browser $browser)
2929

3030
public function webSocketMessage(ConnectionInterface $connection)
3131
{
32-
$this->initializeStatistics($connection->app->id);
33-
34-
$this->statistics[$connection->app->id]->webSocketMessage();
32+
$this
33+
->findOrMakeStatisticForAppId($connection->app->id)
34+
->webSocketMessage();
3535
}
3636

3737
public function apiMessage($appId)
3838
{
39-
$this->initializeStatistics($appId);
40-
41-
$this->statistics[$appId]->apiMessage();
39+
$this
40+
->findOrMakeStatisticForAppId($appId)
41+
->apiMessage();
4242
}
4343

4444
public function connection(ConnectionInterface $connection)
4545
{
46-
$this->initializeStatistics($connection->app->id);
47-
48-
$this->statistics[$connection->app->id]->connection();
46+
$this
47+
->findOrMakeStatisticForAppId($connection->app->id)
48+
->connection();
4949
}
5050

5151
public function disconnection(ConnectionInterface $connection)
5252
{
53-
$this->initializeStatistics($connection->app->id);
54-
55-
$this->statistics[$connection->app->id]->disconnection();
53+
$this
54+
->findOrMakeStatisticForAppId($connection->app->id)
55+
->disconnection();
5656
}
5757

58-
protected function initializeStatistics($id)
58+
protected function findOrMakeStatisticForAppId($appId): Statistic
5959
{
60-
if (!isset($this->statistics[$id])) {
61-
$this->statistics[$id] = new Statistic($id);
60+
if (!isset($this->statistics[$appId])) {
61+
$this->statistics[$appId] = new Statistic($appId);
6262
}
63+
64+
return $this->statistics[$appId];
6365
}
6466

6567
public function save()
@@ -70,19 +72,15 @@ public function save()
7072
continue;
7173
}
7274

73-
$this->browser
75+
$this
76+
->browser
7477
->post(
7578
action([WebSocketStatisticsEntriesController::class, 'store']),
7679
['Content-Type' => 'application/json'],
7780
stream_for(json_encode($statistic->toArray()))
7881
);
7982

80-
// Reset connection and message count
81-
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
82-
->sum(function ($channel) {
83-
return count($channel->getSubscribedConnections());
84-
});
85-
83+
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
8684
$statistic->reset($currentConnectionCount);
8785
}
8886
}

src/WebSockets/Channels/ChannelManager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function getChannels(string $appId): array
4646
return $this->channels[$appId] ?? [];
4747
}
4848

49+
public function getConnectionCount(string $appId): int
50+
{
51+
return collect($this->getChannels($appId))
52+
->sum->getSubscribedConnections();
53+
}
54+
4955
public function removeFromAllChannels(ConnectionInterface $connection)
5056
{
5157
if (!isset($connection->app)) {

src/WebSocketsServiceProvider.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@ class WebSocketsServiceProvider extends ServiceProvider
2222
public function boot()
2323
{
2424
$this->publishes([
25-
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
25+
__DIR__ . '/../config/websockets.php' => base_path('config/websockets.php'),
2626
], 'config');
2727

28-
if (! class_exists('CreateWebSocketsStatisticsEntries')) {
28+
if (!class_exists('CreateWebSocketsStatisticsEntries')) {
2929
$this->publishes([
30-
__DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_statistics_entries_table.php'),
30+
__DIR__ . '/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_websockets_statistics_entries_table.php'),
3131
], 'migrations');
3232
}
3333

34-
$this->registerRouteMacro();
34+
$this
35+
->registerRouteMacro()
36+
->registerStatisticRoute()
37+
->registerDashboardGate();
3538

36-
$this->registerStatisticRoute();
37-
38-
$this->registerDashboardGate();
39-
40-
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
39+
$this->loadViewsFrom(__DIR__ . '/../resources/views/', 'websockets');
4140

4241
$this->commands([
4342
Console\StartWebSocketServer::class,
@@ -46,44 +45,50 @@ public function boot()
4645

4746
public function register()
4847
{
49-
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
48+
$this->mergeConfigFrom(__DIR__ . '/../config/websockets.php', 'websockets');
5049

51-
$this->app->singleton('websockets.router', function() {
50+
$this->app->singleton('websockets.router', function () {
5251
return new Router();
5352
});
5453

55-
$this->app->singleton(ChannelManager::class, function() {
54+
$this->app->singleton(ChannelManager::class, function () {
5655
return new ChannelManager();
5756
});
5857

59-
$this->app->singleton(AppProvider::class, function() {
58+
$this->app->singleton(AppProvider::class, function () {
6059
return app(config('websockets.app_provider'));
6160
});
6261
}
6362

6463
protected function registerRouteMacro()
6564
{
66-
Route::macro('webSockets', function($prefix = 'laravel-websockets') {
67-
Route::prefix($prefix)->namespace('\\')->middleware(Authorize::class)->group(function() {
68-
Route::get('/', ShowDashboard::class);
69-
Route::get('/api/{appId}/statistics', DashboardApiController::class.'@getStatistics');
65+
Route::macro('webSockets', function ($prefix = 'laravel-websockets') {
66+
Route::prefix($prefix)->namespace('\\')->middleware(Authorize::class)->group(function () {
67+
Route::get('/', ShowDashboard::class);
68+
Route::get('/api/{appId}/statistics', DashboardApiController::class . '@getStatistics');
7069
Route::post('auth', AuthenticateDashboard::class);
7170
Route::post('event', SendMessage::class);
7271
});
7372
});
73+
74+
return $this;
7475
}
7576

7677
protected function registerStatisticRoute()
7778
{
78-
Route::prefix('/laravel-websockets')->namespace('\\')->group(function() {
79+
Route::prefix('/laravel-websockets')->namespace('\\')->group(function () {
7980
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
8081
});
82+
83+
return $this;
8184
}
8285

8386
protected function registerDashboardGate()
8487
{
8588
Gate::define('viewWebSocketsDashboard', function ($user = null) {
8689
return app()->environment('local');
8790
});
91+
92+
return $this;
8893
}
8994
}

0 commit comments

Comments
 (0)