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

Commit d11daad

Browse files
authored
Merge pull request #284 from zaxxo/master
[feature] Add restart command for WebSocket server
2 parents 79ef005 + 0915132 commit d11daad

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
@@ -13,6 +13,7 @@
1313
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
1414
use Clue\React\Buzz\Browser;
1515
use Illuminate\Console\Command;
16+
use Illuminate\Support\Facades\Cache;
1617
use React\Dns\Config\Config as DnsConfig;
1718
use React\Dns\Resolver\Factory as DnsFactory;
1819
use React\Dns\Resolver\ResolverInterface;
@@ -28,6 +29,9 @@ class StartWebSocketServer extends Command
2829
/** @var \React\EventLoop\LoopInterface */
2930
protected $loop;
3031

32+
/** @var int */
33+
protected $lastRestart;
34+
3135
public function __construct()
3236
{
3337
parent::__construct();
@@ -42,6 +46,7 @@ public function handle()
4246
->configureHttpLogger()
4347
->configureMessageLogger()
4448
->configureConnectionLogger()
49+
->configureRestartTimer()
4550
->registerEchoRoutes()
4651
->registerCustomRoutes()
4752
->startWebSocketServer();
@@ -105,6 +110,19 @@ protected function configureConnectionLogger()
105110
return $this;
106111
}
107112

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

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)