Merge branch 'master' into schema-check-breaks

This commit is contained in:
Chaitali Sakhale 2020-08-13 21:00:33 +05:30
commit 37719bf77a
10 changed files with 94 additions and 11 deletions

View File

@ -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

View File

@ -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.
*/

View File

@ -111,6 +111,7 @@ window.Echo = new Echo({
key: 'your-pusher-key',
wsHost: window.location.hostname,
wsPort: 6001,
forceTLS: false,
disableStats: true,
});
```

View File

@ -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
});
```

View File

@ -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.
Make sure to take a look at the [Deployment Tips](/docs/laravel-websockets/faq/deploying) to find out how to improve your specific setup.

View File

@ -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,

View File

@ -0,0 +1,23 @@
<?php
namespace BeyondCode\LaravelWebSockets\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\InteractsWithTime;
class RestartWebSocketServer extends Command
{
use InteractsWithTime;
protected $signature = 'websockets:restart';
protected $description = 'Restart the Laravel WebSocket Server';
public function handle()
{
Cache::forever('beyondcode:websockets:restart', $this->currentTime());
$this->info('Broadcasting WebSocket server restart signal.');
}
}

View File

@ -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);
}
}

View File

@ -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,
]);
}

View File

@ -0,0 +1,23 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Commands;
use Artisan;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\InteractsWithTime;
class RestartWebSocketServerTest extends TestCase
{
use InteractsWithTime;
/** @test */
public function it_can_broadcast_restart_signal()
{
$start = $this->currentTime();
Artisan::call('websockets:restart');
$this->assertGreaterThanOrEqual($start, Cache::get('beyondcode:websockets:restart', 0));
}
}