diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3450056..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: | @@ -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 diff --git a/config/websockets.php b/config/websockets.php index 6a2e7f0..45415d7 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\HttpStatisticsLogger::class, + /* * Here you can specify the interval in seconds at which statistics should be logged. */ 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, }); ``` 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 }); ``` 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. diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index d20672b..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: '/{{ request()->path() }}/auth', + authEndpoint: '{{ url(request()->path().'/auth') }}', auth: { headers: { 'X-CSRF-Token': "{{ csrf_token() }}", @@ -162,7 +162,7 @@ }, loadChart() { - $.getJSON('/{{ request()->path() }}/api/'+this.app.id+'/statistics', (data) => { + $.getJSON('{{ url(request()->path().'/api') }}/' + this.app.id + '/statistics', (data) => { let chartData = [ { @@ -246,7 +246,7 @@ }, sendEvent() { - $.post('/{{ request()->path() }}/event', { + $.post('{{ url(request()->path().'/event') }}', { _token: '{{ csrf_token() }}', key: this.app.key, secret: this.app.secret, 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..9c51470 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -9,11 +9,11 @@ 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; 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 +29,9 @@ class StartWebSocketServer extends Command /** @var \React\EventLoop\LoopInterface */ protected $loop; + /** @var int */ + protected $lastRestart; + public function __construct() { parent::__construct(); @@ -43,6 +46,7 @@ class StartWebSocketServer extends Command ->configureHttpLogger() ->configureMessageLogger() ->configureConnectionLogger() + ->configureRestartTimer() ->registerEchoRoutes() ->registerCustomRoutes() ->startWebSocketServer(); @@ -61,7 +65,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\HttpStatisticsLogger::class); + + return new $class(app(ChannelManager::class), $browser); }); $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { @@ -104,6 +110,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 +169,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 02ede2c..cf32f34 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -15,6 +15,7 @@ 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\Facades\Schema; use Illuminate\Support\ServiceProvider; 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('CreateWebSocketsStatisticsEntries')) { + 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'); @@ -40,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)); + } +}