Skip to content
This repository was archived by the owner on Jan 20, 2021. It is now read-only.

Commit 66c751b

Browse files
Initial commit
0 parents  commit 66c751b

6 files changed

+183
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/vendor
3+
composer.lock

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "zaxxo/laravel-websockets-restart",
3+
"description": "This package adds a restart command for the WebSocket server from beyondcode.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Zaxxo",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.1",
14+
"beyondcode/laravel-websockets": "^1.0",
15+
"illuminate/console": "^5.7|^6",
16+
"illuminate/support": "^5.7|^6"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Zaxxo\\LaravelWebSocketsRestart\\": "src/"
21+
}
22+
},
23+
"extra": {
24+
"laravel": {
25+
"providers": [
26+
"Zaxxo\\LaravelWebSocketsRestart\\ServiceProvider"
27+
]
28+
}
29+
}
30+
}

readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Laravel WebSockets Restart
2+
3+
This adds a console command to restart the [Laravel WebSocket](https://github.com/beyondcode/laravel-websockets) server.
4+
5+
Helpful if you deploy automatically (e.g. with Laravel Envoyer).
6+
7+
## Installation
8+
9+
```
10+
composer require zaxxo/laravel-websockets-restart
11+
```
12+
13+
## Usage
14+
15+
```
16+
php artisan websockets:restart
17+
```
18+
19+
## Note
20+
21+
This only works if the server is automatically restarted with Supervisor or something else.

src/RestartWebSocketCommand.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Zaxxo\LaravelWebSocketsRestart;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\InteractsWithTime;
8+
9+
/**
10+
* Command for restarting WebSocket server.
11+
*
12+
* @package Zaxxo\LaravelWebSocketsRestart
13+
* @internal
14+
*/
15+
class RestartWebSocketCommand extends Command
16+
{
17+
use InteractsWithTime;
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'websockets:restart';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Restart WebSocket server';
32+
33+
/**
34+
* Execute the console command.
35+
*/
36+
public function handle()
37+
{
38+
Cache::forever('beyondcode:websockets:restart', $this->currentTime());
39+
40+
$this->info('Broadcasting WebSocket server restart signal.');
41+
}
42+
}

src/ServiceProvider.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Zaxxo\LaravelWebSocketsRestart;
4+
5+
use BeyondCode\LaravelWebSockets\Console\StartWebSocketServer;
6+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
8+
/**
9+
* Service provider for Laravel WebSockets Restart package.
10+
*
11+
* @package Zaxxo\LaravelWebSocketsRestart
12+
* @internal
13+
*/
14+
class ServiceProvider extends BaseServiceProvider
15+
{
16+
/**
17+
* Register any application services.
18+
*/
19+
public function register()
20+
{
21+
$this->app->alias(StartWebSocketCommand::class, StartWebSocketServer::class);
22+
}
23+
24+
/**
25+
* Bootstrap any application services.
26+
*/
27+
public function boot(): void
28+
{
29+
$this->commands(RestartWebSocketCommand::class);
30+
}
31+
}

src/StartWebSocketCommand.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Zaxxo\LaravelWebSocketsRestart;
4+
5+
use BeyondCode\LaravelWebSockets\Console\StartWebSocketServer;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
/**
9+
* Extend the command to start the WebSocket server.
10+
*
11+
* @package Zaxxo\LaravelWebSocketsRestart
12+
* @internal
13+
*/
14+
class StartWebSocketCommand extends StartWebSocketServer
15+
{
16+
/**
17+
* The last time restart was requested.
18+
*
19+
* @var int
20+
*/
21+
private $lastRestart;
22+
23+
/**
24+
* Execute the console command.
25+
*/
26+
public function handle()
27+
{
28+
$this->configureRestartTimer();
29+
30+
parent::handle();
31+
}
32+
33+
/**
34+
* Add a timer to check if server should be restarted.
35+
*/
36+
protected function configureRestartTimer(): void
37+
{
38+
$this->lastRestart = $this->getLastRestart();
39+
40+
$this->loop->addPeriodicTimer(10, function () {
41+
if ($this->getLastRestart() != $this->lastRestart) {
42+
$this->loop->stop();
43+
}
44+
});
45+
}
46+
47+
/**
48+
* Get the last restart time.
49+
*
50+
* @return int
51+
*/
52+
private function getLastRestart(): int
53+
{
54+
return Cache::get('beyondcode:websockets:restart', 0);
55+
}
56+
}

0 commit comments

Comments
 (0)