From 70ffe41c868f2e270e171f9dff80a2ccccd10290 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Thu, 21 Feb 2019 12:11:37 -0500 Subject: [PATCH 01/19] 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 334846dba947fb1f440ffc30e8cfe709cf6e0de5 Mon Sep 17 00:00:00 2001 From: Erlang Parasu Date: Sat, 1 Aug 2020 18:12:26 +0800 Subject: [PATCH 02/19] 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 03/19] 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 a752fb9ecc8fabc173bc89ee7f7d8779c4226fab Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 08:30:24 +0300 Subject: [PATCH 04/19] 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 05/19] 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 06/19] 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 07/19] 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 08/19] 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 09/19] 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 10/19] 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 11/19] 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 12/19] 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 13/19] 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 14/19] 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 15/19] 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 16/19] 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 765e772d762eb14458f49f4d2aa0d1d8756e4759 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 13:22:58 +0300 Subject: [PATCH 17/19] wip --- config/websockets.php | 158 +++++++++++++++++------------- src/WebSocketsServiceProvider.php | 11 ++- 2 files changed, 94 insertions(+), 75 deletions(-) diff --git a/config/websockets.php b/config/websockets.php index a4517cf..90de3e7 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -1,14 +1,61 @@ [ + 'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001), + + 'path' => 'laravel-websockets', + + 'middleware' => [ + 'web', + \BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize::class, + ], + + ], + + 'managers' => [ + + /* + |-------------------------------------------------------------------------- + | Application Manager + |-------------------------------------------------------------------------- + | + | An Application manager determines how your websocket server allows + | the use of the TCP protocol based on, for example, a list of allowed + | applications. + | By default, it uses the defined array in the config file, but you can + | anytime implement the same interface as the class and add your own + | custom method to retrieve the apps. + | + */ + + 'app' => \BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class, + + /* + |-------------------------------------------------------------------------- + | Channel Manager + |-------------------------------------------------------------------------- + | + | When users subscribe or unsubscribe from specific channels, + | the connections are stored to keep track of any interaction with the + | WebSocket server. + | You can however add your own implementation that will help the store + | of the channels alongside their connections. + | + */ + + 'channel' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class, ], /* @@ -34,15 +81,6 @@ return [ ], ], - /* - * This class is responsible for finding the apps. The default provider - * will use the apps defined in this config file. - * - * You can create a custom provider by implementing the - * `AppProvider` interface. - */ - 'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class, - /* * This array contains the hosts of which you want to allow incoming requests. * Leave this empty if you want to accept requests from all hosts. @@ -56,55 +94,6 @@ return [ */ 'max_request_size_in_kb' => 250, - /* - * This path will be used to register the necessary routes for the package. - */ - 'path' => 'laravel-websockets', - - /* - * Dashboard Routes Middleware - * - * These middleware will be assigned to every dashboard route, giving you - * the chance to add your own middleware to this list or change any of - * the existing middleware. Or, you can simply stick with this list. - */ - 'middleware' => [ - 'web', - Authorize::class, - ], - - 'statistics' => [ - /* - * This model will be used to store the statistics of the WebSocketsServer. - * The only requirement is that the model should extend - * `WebSocketsStatisticsEntry` provided by this package. - */ - '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. - */ - 'interval_in_seconds' => 60, - - /* - * When the clean-command is executed, all recorded statistics older than - * the number of days specified here will be deleted. - */ - 'delete_statistics_older_than_days' => 60, - - /* - * Use an DNS resolver to make the requests to the statistics logger - * default is to resolve everything to 127.0.0.1. - */ - 'perform_dns_lookup' => false, - ], - /* * Define the optional SSL context for your WebSocket connections. * You can see all available options at: http://php.net/manual/en/context.ssl.php @@ -130,12 +119,41 @@ return [ 'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null), ], - /* - * Channel Manager - * This class handles how channel persistence is handled. - * By default, persistence is stored in an array by the running webserver. - * The only requirement is that the class should implement - * `ChannelManager` interface provided by this package. - */ - 'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class, + 'statistics' => [ + + /* + * This model will be used to store the statistics of the WebSocketsServer. + * The only requirement is that the model should extend + * `WebSocketsStatisticsEntry` provided by this package. + */ + '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. + */ + + 'interval_in_seconds' => 60, + + /* + * When the clean-command is executed, all recorded statistics older than + * the number of days specified here will be deleted. + */ + + 'delete_statistics_older_than_days' => 60, + + /* + * Use an DNS resolver to make the requests to the statistics logger + * default is to resolve everything to 127.0.0.1. + */ + + 'perform_dns_lookup' => false, + ], + ]; diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index cf32f34..c68379a 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -54,19 +54,20 @@ class WebSocketsServiceProvider extends ServiceProvider }); $this->app->singleton(ChannelManager::class, function () { - return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager')) - ? app(config('websockets.channel_manager')) : new ArrayChannelManager(); + $channelManager = config('websockets.managers.channel', ArrayChannelManager::class); + + return new $channelManager; }); $this->app->singleton(AppProvider::class, function () { - return app(config('websockets.app_provider')); + return app(config('websockets.managers.app')); }); } protected function registerRoutes() { - Route::prefix(config('websockets.path'))->group(function () { - Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () { + Route::prefix(config('websockets.dashboard.path'))->group(function () { + Route::middleware(config('websockets.dashboard.middleware', [AuthorizeDashboard::class]))->group(function () { Route::get('/', ShowDashboard::class); Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']); Route::post('auth', AuthenticateDashboard::class); From f32ae78888536da702677a976419add737a2884f Mon Sep 17 00:00:00 2001 From: rennokki Date: Thu, 13 Aug 2020 13:23:21 +0300 Subject: [PATCH 18/19] Apply fixes from StyleCI (#445) --- config/websockets.php | 1 - 1 file changed, 1 deletion(-) diff --git a/config/websockets.php b/config/websockets.php index 90de3e7..01ee97b 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -132,7 +132,6 @@ 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, /* From 8c393c76c3de760737de6343b13f0980650b3e98 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 13 Aug 2020 13:34:12 +0300 Subject: [PATCH 19/19] wip --- config/websockets.php | 162 +++++++++++++++++++-------- src/Console/StartWebSocketServer.php | 5 +- 2 files changed, 117 insertions(+), 50 deletions(-) diff --git a/config/websockets.php b/config/websockets.php index 90de3e7..2f3f321 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -56,18 +56,24 @@ return [ */ 'channel' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class, + ], /* - * This package comes with multi tenancy out of the box. Here you can - * configure the different apps that can use the webSockets server. - * - * Optionally you specify capacity so you can limit the maximum - * concurrent connections for a specific app. - * - * Optionally you can disable client events so clients cannot send - * messages to each other via the webSockets. - */ + |-------------------------------------------------------------------------- + | Applications Repository + |-------------------------------------------------------------------------- + | + | By default, the only allowed app is the one you define with + | your PUSHER_* variables from .env. + | You can configure to use multiple apps if you need to, or use + | a custom App Manager that will handle the apps from a database, per se. + | + | You can apply multiple settings, like the maximum capacity, enable + | client-to-client messages or statistics. + | + */ + 'apps' => [ [ 'id' => env('PUSHER_APP_ID'), @@ -82,78 +88,142 @@ return [ ], /* - * This array contains the hosts of which you want to allow incoming requests. - * Leave this empty if you want to accept requests from all hosts. - */ + |-------------------------------------------------------------------------- + | Allowed Origins + |-------------------------------------------------------------------------- + | + | If not empty, you can whitelist certain origins that will be allowed + | to connect to the websocket server. + | + */ + 'allowed_origins' => [ // ], /* - * The maximum request size in kilobytes that is allowed for an incoming WebSocket request. - */ + |-------------------------------------------------------------------------- + | Maximum Request Size + |-------------------------------------------------------------------------- + | + | The maximum request size in kilobytes that is allowed for + | an incoming WebSocket request. + | + */ + 'max_request_size_in_kb' => 250, /* - * Define the optional SSL context for your WebSocket connections. - * You can see all available options at: http://php.net/manual/en/context.ssl.php - */ + |-------------------------------------------------------------------------- + | SSL Configuration + |-------------------------------------------------------------------------- + | + | By default, the configuration allows only on HTTP. For SSL, you need + | to set up the the certificate, the key, and optionally, the passphrase + | for the private key. + | You will need to restart the server for the settings to take place. + | + */ + 'ssl' => [ - /* - * Path to local certificate file on filesystem. It must be a PEM encoded file which - * contains your certificate and private key. It can optionally contain the - * certificate chain of issuers. The private key also may be contained - * in a separate file specified by local_pk. - */ + 'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null), - /* - * Path to local private key file on filesystem in case of separate files for - * certificate (local_cert) and private key. - */ + 'capath' => env('LARAVEL_WEBSOCKETS_SSL_CA', null), + 'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null), - /* - * Passphrase for your local_cert file. - */ 'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null), + + 'verify_peer' => env('APP_ENV') === 'production', + + 'allow_self_signed' => env('APP_ENV') !== 'production', + ], 'statistics' => [ /* - * This model will be used to store the statistics of the WebSocketsServer. - * The only requirement is that the model should extend - * `WebSocketsStatisticsEntry` provided by this package. - */ + |-------------------------------------------------------------------------- + | Statistics Eloquent Model + |-------------------------------------------------------------------------- + | + | This model will be used to store the statistics of the WebSocketsServer. + | The only requirement is that the model should extend + | `WebSocketsStatisticsEntry` provided by this package. + | + */ + '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. - */ + /* + |-------------------------------------------------------------------------- + | Statistics Logger Handler + |-------------------------------------------------------------------------- + | + | The Statistics Logger will, by default, handle the incoming statistics, + | store them into an array and then store them into the database + | on each interval. + | + */ 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger::class, /* - * Here you can specify the interval in seconds at which statistics should be logged. - */ + |-------------------------------------------------------------------------- + | Statistics Interval Period + |-------------------------------------------------------------------------- + | + | Here you can specify the interval in seconds at which + | statistics should be logged. + | + */ 'interval_in_seconds' => 60, /* - * When the clean-command is executed, all recorded statistics older than - * the number of days specified here will be deleted. - */ + |-------------------------------------------------------------------------- + | Statistics Deletion Period + |-------------------------------------------------------------------------- + | + | When the clean-command is executed, all recorded statistics older than + | the number of days specified here will be deleted. + | + */ 'delete_statistics_older_than_days' => 60, /* - * Use an DNS resolver to make the requests to the statistics logger - * default is to resolve everything to 127.0.0.1. - */ + |-------------------------------------------------------------------------- + | DNS Lookup + |-------------------------------------------------------------------------- + | + | Use an DNS resolver to make the requests to the statistics logger + | default is to resolve everything to 127.0.0.1. + | + */ 'perform_dns_lookup' => false, + + /* + |-------------------------------------------------------------------------- + | DNS Lookup TLS Settings + |-------------------------------------------------------------------------- + | + | You can configure the DNS Lookup Connector the TLS settings. + | Check the available options here: + | https://github.com/reactphp/socket/blob/master/src/Connector.php#L29 + | + */ + + 'tls' => [ + + 'verify_peer' => env('APP_ENV') === 'production', + + 'verify_peer_name' => env('APP_ENV') === 'production', + + ], + ], ]; diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index cd1de64..91a5d8c 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -56,10 +56,7 @@ class StartWebSocketServer extends Command { $connector = new Connector($this->loop, [ 'dns' => $this->getDnsResolver(), - 'tls' => [ - 'verify_peer' => config('app.env') === 'production', - 'verify_peer_name' => config('app.env') === 'production', - ], + 'tls' => config('websockets.statistics.tls'), ]); $browser = new Browser($this->loop, $connector);