Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 0915132

Browse files
Add restart command for WebSocket server
1 parent 4c12fb3 commit 0915132

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\InteractsWithTime;
8+
9+
class RestartWebSocketServer extends Command
10+
{
11+
use InteractsWithTime;
12+
13+
protected $signature = 'websockets:restart';
14+
15+
protected $description = 'Restart the Laravel WebSocket Server';
16+
17+
public function handle()
18+
{
19+
Cache::forever('beyondcode:websockets:restart', $this->currentTime());
20+
21+
$this->info('Broadcasting WebSocket server restart signal.');
22+
}
23+
}

src/Console/StartWebSocketServer.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
1515
use Clue\React\Buzz\Browser;
1616
use Illuminate\Console\Command;
17+
use Illuminate\Support\Facades\Cache;
1718
use React\Dns\Config\Config as DnsConfig;
1819
use React\Dns\Resolver\Factory as DnsFactory;
1920
use React\Dns\Resolver\ResolverInterface;
@@ -29,6 +30,9 @@ class StartWebSocketServer extends Command
2930
/** @var \React\EventLoop\LoopInterface */
3031
protected $loop;
3132

33+
/** @var int */
34+
protected $lastRestart;
35+
3236
public function __construct()
3337
{
3438
parent::__construct();
@@ -43,6 +47,7 @@ public function handle()
4347
->configureHttpLogger()
4448
->configureMessageLogger()
4549
->configureConnectionLogger()
50+
->configureRestartTimer()
4651
->registerEchoRoutes()
4752
->registerCustomRoutes()
4853
->startWebSocketServer();
@@ -104,6 +109,19 @@ protected function configureConnectionLogger()
104109
return $this;
105110
}
106111

112+
public function configureRestartTimer()
113+
{
114+
$this->lastRestart = $this->getLastRestart();
115+
116+
$this->loop->addPeriodicTimer(10, function () {
117+
if ($this->getLastRestart() !== $this->lastRestart) {
118+
$this->loop->stop();
119+
}
120+
});
121+
122+
return $this;
123+
}
124+
107125
protected function registerEchoRoutes()
108126
{
109127
WebSocketsRouter::echo();
@@ -150,4 +168,9 @@ protected function getDnsResolver(): ResolverInterface
150168
$this->loop
151169
);
152170
}
171+
172+
protected function getLastRestart()
173+
{
174+
return Cache::get('beyondcode:websockets:restart', 0);
175+
}
153176
}

src/WebSocketsServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function boot()
4141
$this->commands([
4242
Console\StartWebSocketServer::class,
4343
Console\CleanStatistics::class,
44+
Console\RestartWebSocketServer::class,
4445
]);
4546
}
4647

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Commands;
4+
5+
use Artisan;
6+
use BeyondCode\LaravelWebSockets\Tests\TestCase;
7+
use Illuminate\Support\Facades\Cache;
8+
use Illuminate\Support\InteractsWithTime;
9+
10+
class RestartWebSocketServerTest extends TestCase
11+
{
12+
use InteractsWithTime;
13+
14+
/** @test */
15+
public function it_can_broadcast_restart_signal()
16+
{
17+
$start = $this->currentTime();
18+
19+
Artisan::call('websockets:restart');
20+
21+
$this->assertGreaterThanOrEqual($start, Cache::get('beyondcode:websockets:restart', 0));
22+
}
23+
}

0 commit comments

Comments
 (0)