Add restart command for WebSocket server
This commit is contained in:
parent
4c12fb3490
commit
0915132369
|
|
@ -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.');
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ class WebSocketsServiceProvider extends ServiceProvider
|
|||
$this->commands([
|
||||
Console\StartWebSocketServer::class,
|
||||
Console\CleanStatistics::class,
|
||||
Console\RestartWebSocketServer::class,
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue