From 70ffe41c868f2e270e171f9dff80a2ccccd10290 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Thu, 21 Feb 2019 12:11:37 -0500 Subject: [PATCH 01/27] Add support for overriding stats logger's base URL --- config/websockets.php | 8 ++++++++ src/Statistics/Logger/HttpStatisticsLogger.php | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config/websockets.php b/config/websockets.php index 74075b5..8543b99 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -80,6 +80,14 @@ return [ */ 'delete_statistics_older_than_days' => 60, + /* + * By default, the websockets server attempts to connect to whatever + * your APP_URL is set to. If running in a more complex environment, + * you may wish to override the base URL for internal requests to + * allow statistics to be collected. + */ + 'base_url_override' => null, + /* * Use an DNS resolver to make the requests to the statistics logger * default is to resolve everything to 127.0.0.1. diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index a23f7a9..ef61b94 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -65,6 +65,19 @@ class HttpStatisticsLogger implements StatisticsLogger return $this->statistics[$appId]; } + protected function getUrl(): string + { + $action = [WebSocketStatisticsEntriesController::class, 'store']; + + $url_override = config('websockets.statistics.base_url_override', null); + + if ($url_override !== null) { + return $url_override.action($action, [], false); + } + + return action($action); + } + public function save() { foreach ($this->statistics as $appId => $statistic) { @@ -79,7 +92,7 @@ class HttpStatisticsLogger implements StatisticsLogger $this ->browser ->post( - action([WebSocketStatisticsEntriesController::class, 'store']), + $this->getUrl(), ['Content-Type' => 'application/json'], stream_for(json_encode($postData)) ); From acf349602d948852c1511ac33049127c15351678 Mon Sep 17 00:00:00 2001 From: Marcus Olsson Date: Tue, 16 Jun 2020 09:47:41 +0200 Subject: [PATCH 02/27] Fix broken link in FAQ --- docs/faq/scaling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/faq/scaling.md b/docs/faq/scaling.md index f786d90..f3768d3 100644 --- a/docs/faq/scaling.md +++ b/docs/faq/scaling.md @@ -15,4 +15,4 @@ Here is another benchmark that was run on a 2GB Digital Ocean droplet with 2 CPU ![Benchmark](/img/simultaneous_users_2gb.png) -Make sure to take a look at the [Deployment Tips](/1.0/faq/deploying.html) to find out how to improve your specific setup. \ No newline at end of file +Make sure to take a look at the [Deployment Tips](/docs/laravel-websockets/faq/deploying) to find out how to improve your specific setup. From 45d951c56b45589c02aa9382f9903ac4a388ad8d Mon Sep 17 00:00:00 2001 From: robindrost Date: Tue, 30 Jun 2020 14:48:28 +0200 Subject: [PATCH 03/27] Update statistics table name check Class name was missing Table, this result into creating new migrations everytime you publish the package. --- src/WebSocketsServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 02ede2c..3db2939 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -25,7 +25,7 @@ class WebSocketsServiceProvider extends ServiceProvider __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), ], 'config'); - if (! class_exists('CreateWebSocketsStatisticsEntries')) { + if (! class_exists('CreateWebSocketsStatisticsEntriesTable')) { $this->publishes([ __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'), ], 'migrations'); From 334846dba947fb1f440ffc30e8cfe709cf6e0de5 Mon Sep 17 00:00:00 2001 From: Erlang Parasu Date: Sat, 1 Aug 2020 18:12:26 +0800 Subject: [PATCH 04/27] getBasePath --- resources/views/dashboard.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index d20672b..4a503a1 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -118,7 +118,7 @@ wssPort: this.port === null ? 6001 : this.port, wsPath: this.app.path === null ? '' : this.app.path, disableStats: true, - authEndpoint: '/{{ request()->path() }}/auth', + authEndpoint: '/{{ request()->getBasePath() }}/auth', auth: { headers: { 'X-CSRF-Token': "{{ csrf_token() }}", @@ -162,7 +162,7 @@ }, loadChart() { - $.getJSON('/{{ request()->path() }}/api/'+this.app.id+'/statistics', (data) => { + $.getJSON('/{{ request()->getBasePath() }}/api/'+this.app.id+'/statistics', (data) => { let chartData = [ { @@ -246,7 +246,7 @@ }, sendEvent() { - $.post('/{{ request()->path() }}/event', { + $.post('/{{ request()->getBasePath() }}/event', { _token: '{{ csrf_token() }}', key: this.app.key, secret: this.app.secret, From 14b9b11836496662e4cbd4e97aaf4ee379c6c152 Mon Sep 17 00:00:00 2001 From: Erlang Parasu Date: Sat, 1 Aug 2020 18:29:48 +0800 Subject: [PATCH 05/27] Update dashboard.blade.php --- resources/views/dashboard.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 4a503a1..f19b83e 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -118,7 +118,7 @@ wssPort: this.port === null ? 6001 : this.port, wsPath: this.app.path === null ? '' : this.app.path, disableStats: true, - authEndpoint: '/{{ request()->getBasePath() }}/auth', + authEndpoint: '{{ request()->getBasePath() }}/{{ request()->path() }}/auth', auth: { headers: { 'X-CSRF-Token': "{{ csrf_token() }}", @@ -162,7 +162,7 @@ }, loadChart() { - $.getJSON('/{{ request()->getBasePath() }}/api/'+this.app.id+'/statistics', (data) => { + $.getJSON('{{ request()->getBasePath() }}/{{ request()->path() }}/api/'+this.app.id+'/statistics', (data) => { let chartData = [ { @@ -246,7 +246,7 @@ }, sendEvent() { - $.post('/{{ request()->getBasePath() }}/event', { + $.post('{{ request()->getBasePath() }}/{{ request()->path() }}/event', { _token: '{{ csrf_token() }}', key: this.app.key, secret: this.app.secret, From ab0ac05de77555eb0aa7e0063cf4d9ec290a3ab4 Mon Sep 17 00:00:00 2001 From: JuanDMeGon Date: Sat, 1 Aug 2020 17:09:56 -0500 Subject: [PATCH 06/27] The encrypted property was removed on pusher-js 6 the encrypted property was removed in favor of forceTLS one, officially on pusher-js 6. --- docs/basic-usage/ssl.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/basic-usage/ssl.md b/docs/basic-usage/ssl.md index a8265d8..7e700d8 100644 --- a/docs/basic-usage/ssl.md +++ b/docs/basic-usage/ssl.md @@ -49,7 +49,7 @@ php artisan websockets:serve ## Client configuration When your SSL settings are in place and working, you still need to tell Laravel Echo that it should make use of it. -You can do this by specifying the `encrypted` property in your JavaScript file, like this: +You can do this by specifying the `forceTLS` property in your JavaScript file, like this: ```js import Echo from "laravel-echo" @@ -62,7 +62,7 @@ window.Echo = new Echo({ wsHost: window.location.hostname, wsPort: 6001, disableStats: true, - encrypted: true + forceTLS: true }); ``` From 1fdcaa6a7b60e5c23d746dad2f2ac237e3d8770b Mon Sep 17 00:00:00 2001 From: JuanDMeGon Date: Sat, 1 Aug 2020 17:19:57 -0500 Subject: [PATCH 07/27] After pusher-js 6 forceTLS is default true As now since pusher-js 6 the encrypted attribute was removed in favor of forceTLS and it is true by default, so we need to use forceTLS: false, to avoid CORS issues. --- docs/basic-usage/pusher.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/basic-usage/pusher.md b/docs/basic-usage/pusher.md index e9b7a54..7c07e03 100644 --- a/docs/basic-usage/pusher.md +++ b/docs/basic-usage/pusher.md @@ -111,6 +111,7 @@ window.Echo = new Echo({ key: 'your-pusher-key', wsHost: window.location.hostname, wsPort: 6001, + forceTLS: false, disableStats: true, }); ``` From 8285f6da17ff702f15129f505631c06aac1dc798 Mon Sep 17 00:00:00 2001 From: robindrost Date: Wed, 12 Aug 2020 12:26:07 +0200 Subject: [PATCH 08/27] Update WebSocketsServiceProvider.php Use schema check instead of class name check. --- src/WebSocketsServiceProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 3db2939..cbc188d 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -16,6 +16,7 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChanne use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades\Schema class WebSocketsServiceProvider extends ServiceProvider { @@ -25,7 +26,7 @@ class WebSocketsServiceProvider extends ServiceProvider __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), ], 'config'); - if (! class_exists('CreateWebSocketsStatisticsEntriesTable')) { + if (!Schema::hasTable('websockets_statistics_entries')) $this->publishes([ __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'), ], 'migrations'); From c735e70009c3bcdd5a86e7cdbc2176ca089b3ca8 Mon Sep 17 00:00:00 2001 From: robindrost Date: Wed, 12 Aug 2020 12:27:04 +0200 Subject: [PATCH 09/27] Update WebSocketsServiceProvider.php Add missing ;. --- src/WebSocketsServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index cbc188d..f804cd7 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -16,7 +16,7 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChanne use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; -use Illuminate\Support\Facades\Schema +use Illuminate\Support\Facades\Schema; class WebSocketsServiceProvider extends ServiceProvider { From 5b11224865b50c422f14cce07a39d898fc180272 Mon Sep 17 00:00:00 2001 From: robindrost Date: Wed, 12 Aug 2020 12:27:53 +0200 Subject: [PATCH 10/27] Update WebSocketsServiceProvider.php Add missing { --- src/WebSocketsServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index f804cd7..ec0c245 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -26,7 +26,7 @@ class WebSocketsServiceProvider extends ServiceProvider __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), ], 'config'); - if (!Schema::hasTable('websockets_statistics_entries')) + if (!Schema::hasTable('websockets_statistics_entries')) { $this->publishes([ __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'), ], 'migrations'); From dd05008dd24a2c4d5d6af3555385ab0d36b30a73 Mon Sep 17 00:00:00 2001 From: robindrost Date: Wed, 12 Aug 2020 12:28:56 +0200 Subject: [PATCH 11/27] Update WebSocketsServiceProvider.php Namespace import order. --- src/WebSocketsServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index ec0c245..00155d5 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -15,8 +15,8 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; -use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\ServiceProvider; class WebSocketsServiceProvider extends ServiceProvider { From 13d8d9491d03ccde5234070eafeed968b31c0bd1 Mon Sep 17 00:00:00 2001 From: robindrost Date: Wed, 12 Aug 2020 12:29:32 +0200 Subject: [PATCH 12/27] Update WebSocketsServiceProvider.php Add missing space. --- src/WebSocketsServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 00155d5..a9a7ff9 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -26,7 +26,7 @@ class WebSocketsServiceProvider extends ServiceProvider __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), ], 'config'); - if (!Schema::hasTable('websockets_statistics_entries')) { + if (! Schema::hasTable('websockets_statistics_entries')) { $this->publishes([ __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'), ], 'migrations'); From a752fb9ecc8fabc173bc89ee7f7d8779c4226fab Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 08:30:24 +0300 Subject: [PATCH 13/27] formatting --- src/Statistics/Logger/HttpStatisticsLogger.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index ef61b94..ea14bc2 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -69,10 +69,10 @@ class HttpStatisticsLogger implements StatisticsLogger { $action = [WebSocketStatisticsEntriesController::class, 'store']; - $url_override = config('websockets.statistics.base_url_override', null); + $overridenUrl = config('websockets.statistics.base_url_override'); - if ($url_override !== null) { - return $url_override.action($action, [], false); + if ($overridenUrl) { + return $overridenUrl.action($action, [], false); } return action($action); From 99c6c068970bf2ae8dad8ac45dbf2097f68f3203 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 08:32:20 +0300 Subject: [PATCH 14/27] formatting --- .../Logger/HttpStatisticsLogger.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index ea14bc2..0197c38 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -65,19 +65,6 @@ class HttpStatisticsLogger implements StatisticsLogger return $this->statistics[$appId]; } - protected function getUrl(): string - { - $action = [WebSocketStatisticsEntriesController::class, 'store']; - - $overridenUrl = config('websockets.statistics.base_url_override'); - - if ($overridenUrl) { - return $overridenUrl.action($action, [], false); - } - - return action($action); - } - public function save() { foreach ($this->statistics as $appId => $statistic) { @@ -92,7 +79,7 @@ class HttpStatisticsLogger implements StatisticsLogger $this ->browser ->post( - $this->getUrl(), + $this->storeStatisticsUrl(), ['Content-Type' => 'application/json'], stream_for(json_encode($postData)) ); @@ -101,4 +88,17 @@ class HttpStatisticsLogger implements StatisticsLogger $statistic->reset($currentConnectionCount); } } + + protected function storeStatisticsUrl(): string + { + $action = [WebSocketStatisticsEntriesController::class, 'store']; + + $overridenUrl = config('websockets.statistics.base_url_override'); + + if ($overridenUrl) { + return $overridenUrl.action($action, [], false); + } + + return action($action); + } } From 371bf5418c1021ede09cb48d5a5a3ee5341ca3e8 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 09:47:55 +0300 Subject: [PATCH 15/27] inline if --- src/Statistics/Logger/HttpStatisticsLogger.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 0197c38..926099e 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -95,10 +95,8 @@ class HttpStatisticsLogger implements StatisticsLogger $overridenUrl = config('websockets.statistics.base_url_override'); - if ($overridenUrl) { - return $overridenUrl.action($action, [], false); - } - - return action($action); + return $overridentUrl + ? $overridenUrl.action($action, [], false) + : action($action); } } From 10208671bb8bbf458e62225ad542bfdf42192cc1 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 09:49:09 +0300 Subject: [PATCH 16/27] csfixing --- src/Statistics/Logger/HttpStatisticsLogger.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 926099e..d732025 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -2,13 +2,13 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logger; -use Clue\React\Buzz\Browser; -use Ratchet\ConnectionInterface; -use function GuzzleHttp\Psr7\stream_for; use BeyondCode\LaravelWebSockets\Apps\App; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Statistic; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; +use Clue\React\Buzz\Browser; +use function GuzzleHttp\Psr7\stream_for; +use Ratchet\ConnectionInterface; class HttpStatisticsLogger implements StatisticsLogger { From b2b2961f0e29a012b424c1e7c4ec5f65a7ea488a Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 10:09:09 +0300 Subject: [PATCH 17/27] Revert "Add support for overriding stats logger's base URL" --- config/websockets.php | 8 -------- src/Statistics/Logger/HttpStatisticsLogger.php | 13 +------------ 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/config/websockets.php b/config/websockets.php index a0d1516..6a2e7f0 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -92,14 +92,6 @@ return [ */ 'delete_statistics_older_than_days' => 60, - /* - * By default, the websockets server attempts to connect to whatever - * your APP_URL is set to. If running in a more complex environment, - * you may wish to override the base URL for internal requests to - * allow statistics to be collected. - */ - 'base_url_override' => null, - /* * Use an DNS resolver to make the requests to the statistics logger * default is to resolve everything to 127.0.0.1. diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index d732025..1cc0201 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -79,7 +79,7 @@ class HttpStatisticsLogger implements StatisticsLogger $this ->browser ->post( - $this->storeStatisticsUrl(), + action([WebSocketStatisticsEntriesController::class, 'store']), ['Content-Type' => 'application/json'], stream_for(json_encode($postData)) ); @@ -88,15 +88,4 @@ class HttpStatisticsLogger implements StatisticsLogger $statistic->reset($currentConnectionCount); } } - - protected function storeStatisticsUrl(): string - { - $action = [WebSocketStatisticsEntriesController::class, 'store']; - - $overridenUrl = config('websockets.statistics.base_url_override'); - - return $overridentUrl - ? $overridenUrl.action($action, [], false) - : action($action); - } } From 5e0ec9e6a1a7cbeb984710d96aab2a7592c3c1c7 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 10:15:52 +0300 Subject: [PATCH 18/27] Added defined http logger class in the config --- config/websockets.php | 6 ++++++ src/Console/StartWebSocketServer.php | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/config/websockets.php b/config/websockets.php index 6a2e7f0..a4517cf 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -81,6 +81,12 @@ return [ */ 'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class, + /** + * The Statistics Logger will, by default, handle the incoming statistics, store them + * and then release them into the database on each interval defined below. + */ + 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger::class, + /* * Here you can specify the interval in seconds at which statistics should be logged. */ diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index e68767c..1f636c7 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -61,7 +61,9 @@ class StartWebSocketServer extends Command $browser = new Browser($this->loop, $connector); app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) { - return new HttpStatisticsLogger(app(ChannelManager::class), $browser); + $class = config('websockets.statistics.logger') ?: \BeyondCode\LaravelWebSockets\Statistics\Logger::class; + + return new $class(app(ChannelManager::class), $browser); }); $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { From 73dfa02f3dbee00ce04eee6fbe816c7dc7842fcb Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 10:16:15 +0300 Subject: [PATCH 19/27] Apply fixes from StyleCI (#441) --- src/Console/StartWebSocketServer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 1f636c7..81c51a3 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -9,7 +9,6 @@ use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger; use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; -use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger; use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use Clue\React\Buzz\Browser; From 10c2a060860df5488e57af0d4f623194d05691b5 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 10:17:34 +0300 Subject: [PATCH 20/27] wip --- src/Console/StartWebSocketServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 1f636c7..a078666 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -61,7 +61,7 @@ class StartWebSocketServer extends Command $browser = new Browser($this->loop, $connector); app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) { - $class = config('websockets.statistics.logger') ?: \BeyondCode\LaravelWebSockets\Statistics\Logger::class; + $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger::class); return new $class(app(ChannelManager::class), $browser); }); From 9966412d96fa1e1209e985c3112d17ad2e8971bc Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 10:33:01 +0300 Subject: [PATCH 21/27] Added Codecov for unit testing --- .github/workflows/run-tests.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3450056..5fc0e41 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -45,4 +45,8 @@ jobs: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - name: Execute tests - run: vendor/bin/phpunit + run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml + + - uses: codecov/codecov-action@v1 + with: + fail_ci_if_error: false From 43ef7fde932beaa873305f80a14448944aa2b537 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 10:43:31 +0300 Subject: [PATCH 22/27] Using pcov as coverage provider --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 5fc0e41..6d3b074 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -37,7 +37,7 @@ jobs: with: php-version: ${{ matrix.php }} extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick - coverage: none + coverage: pcov - name: Install dependencies run: | From 091513236972c27c91b9675fc86352318690be35 Mon Sep 17 00:00:00 2001 From: zaxxo-dkraemer Date: Wed, 6 Nov 2019 09:25:55 +0100 Subject: [PATCH 23/27] Add restart command for WebSocket server --- src/Console/RestartWebSocketServer.php | 23 +++++++++++++++++++ src/Console/StartWebSocketServer.php | 23 +++++++++++++++++++ src/WebSocketsServiceProvider.php | 1 + tests/Commands/RestartWebSocketServerTest.php | 23 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 src/Console/RestartWebSocketServer.php create mode 100644 tests/Commands/RestartWebSocketServerTest.php diff --git a/src/Console/RestartWebSocketServer.php b/src/Console/RestartWebSocketServer.php new file mode 100644 index 0000000..26d240c --- /dev/null +++ b/src/Console/RestartWebSocketServer.php @@ -0,0 +1,23 @@ +currentTime()); + + $this->info('Broadcasting WebSocket server restart signal.'); + } +} diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index e68767c..e08e736 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -14,6 +14,7 @@ use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as Statistic use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use Clue\React\Buzz\Browser; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Cache; use React\Dns\Config\Config as DnsConfig; use React\Dns\Resolver\Factory as DnsFactory; use React\Dns\Resolver\ResolverInterface; @@ -29,6 +30,9 @@ class StartWebSocketServer extends Command /** @var \React\EventLoop\LoopInterface */ protected $loop; + /** @var int */ + protected $lastRestart; + public function __construct() { parent::__construct(); @@ -43,6 +47,7 @@ class StartWebSocketServer extends Command ->configureHttpLogger() ->configureMessageLogger() ->configureConnectionLogger() + ->configureRestartTimer() ->registerEchoRoutes() ->registerCustomRoutes() ->startWebSocketServer(); @@ -104,6 +109,19 @@ class StartWebSocketServer extends Command return $this; } + public function configureRestartTimer() + { + $this->lastRestart = $this->getLastRestart(); + + $this->loop->addPeriodicTimer(10, function () { + if ($this->getLastRestart() !== $this->lastRestart) { + $this->loop->stop(); + } + }); + + return $this; + } + protected function registerEchoRoutes() { WebSocketsRouter::echo(); @@ -150,4 +168,9 @@ class StartWebSocketServer extends Command $this->loop ); } + + protected function getLastRestart() + { + return Cache::get('beyondcode:websockets:restart', 0); + } } diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index a9a7ff9..cf32f34 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -41,6 +41,7 @@ class WebSocketsServiceProvider extends ServiceProvider $this->commands([ Console\StartWebSocketServer::class, Console\CleanStatistics::class, + Console\RestartWebSocketServer::class, ]); } diff --git a/tests/Commands/RestartWebSocketServerTest.php b/tests/Commands/RestartWebSocketServerTest.php new file mode 100644 index 0000000..e80748a --- /dev/null +++ b/tests/Commands/RestartWebSocketServerTest.php @@ -0,0 +1,23 @@ +currentTime(); + + Artisan::call('websockets:restart'); + + $this->assertGreaterThanOrEqual($start, Cache::get('beyondcode:websockets:restart', 0)); + } +} From ee81f7ad3a87a5e0d07a67bbcb9f4c9c643fdcd5 Mon Sep 17 00:00:00 2001 From: Erlang Parasu Date: Thu, 13 Aug 2020 17:11:57 +0800 Subject: [PATCH 24/27] use function url --- resources/views/dashboard.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index f19b83e..b9c9785 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -118,7 +118,7 @@ wssPort: this.port === null ? 6001 : this.port, wsPath: this.app.path === null ? '' : this.app.path, disableStats: true, - authEndpoint: '{{ request()->getBasePath() }}/{{ request()->path() }}/auth', + authEndpoint: '{{ url("/auth") }}', auth: { headers: { 'X-CSRF-Token': "{{ csrf_token() }}", @@ -162,7 +162,7 @@ }, loadChart() { - $.getJSON('{{ request()->getBasePath() }}/{{ request()->path() }}/api/'+this.app.id+'/statistics', (data) => { + $.getJSON('{{ url("/api") }}/' + this.app.id + '/statistics', (data) => { let chartData = [ { @@ -246,7 +246,7 @@ }, sendEvent() { - $.post('{{ request()->getBasePath() }}/{{ request()->path() }}/event', { + $.post('{{ url("/event") }}', { _token: '{{ csrf_token() }}', key: this.app.key, secret: this.app.secret, From 7fc020d1dc221cd000a96edb556d218fa67d9b69 Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 12:13:07 +0300 Subject: [PATCH 25/27] formatting --- resources/views/dashboard.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index b9c9785..fbf90c7 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -118,7 +118,7 @@ wssPort: this.port === null ? 6001 : this.port, wsPath: this.app.path === null ? '' : this.app.path, disableStats: true, - authEndpoint: '{{ url("/auth") }}', + authEndpoint: '{{ url('/auth') }}', auth: { headers: { 'X-CSRF-Token': "{{ csrf_token() }}", @@ -162,7 +162,7 @@ }, loadChart() { - $.getJSON('{{ url("/api") }}/' + this.app.id + '/statistics', (data) => { + $.getJSON('{{ url('/api') }}/' + this.app.id + '/statistics', (data) => { let chartData = [ { @@ -246,7 +246,7 @@ }, sendEvent() { - $.post('{{ url("/event") }}', { + $.post('{{ url('/event') }}', { _token: '{{ csrf_token() }}', key: this.app.key, secret: this.app.secret, From 679a8d097a9062e56456ab1a860bf80b93964d79 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 14:40:19 +0300 Subject: [PATCH 26/27] Fixed logger --- config/websockets.php | 2 +- src/Console/StartWebSocketServer.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/websockets.php b/config/websockets.php index a4517cf..45415d7 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -85,7 +85,7 @@ return [ * The Statistics Logger will, by default, handle the incoming statistics, store them * and then release them into the database on each interval defined below. */ - 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger::class, + 'logger' => BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class, /* * Here you can specify the interval in seconds at which statistics should be logged. diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index cd1de64..9c51470 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -65,7 +65,7 @@ class StartWebSocketServer extends Command $browser = new Browser($this->loop, $connector); app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) { - $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger::class); + $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class); return new $class(app(ChannelManager::class), $browser); }); From 83830548fd9950d1066e5c0edd8f5d56ab21aa16 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 15:21:55 +0300 Subject: [PATCH 27/27] Fixed the urls for the dashboard API calls --- resources/views/dashboard.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index fbf90c7..58a6426 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -118,7 +118,7 @@ wssPort: this.port === null ? 6001 : this.port, wsPath: this.app.path === null ? '' : this.app.path, disableStats: true, - authEndpoint: '{{ url('/auth') }}', + authEndpoint: '{{ url(request()->path().'/auth') }}', auth: { headers: { 'X-CSRF-Token': "{{ csrf_token() }}", @@ -162,7 +162,7 @@ }, loadChart() { - $.getJSON('{{ url('/api') }}/' + this.app.id + '/statistics', (data) => { + $.getJSON('{{ url(request()->path().'/api') }}/' + this.app.id + '/statistics', (data) => { let chartData = [ { @@ -246,7 +246,7 @@ }, sendEvent() { - $.post('{{ url('/event') }}', { + $.post('{{ url(request()->path().'/event') }}', { _token: '{{ csrf_token() }}', key: this.app.key, secret: this.app.secret,