-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from bangnokia/feat/hot-reloading
LGTM
- Loading branch information
Showing
11 changed files
with
337 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace BangNokia\Lina\Commands; | ||
|
||
use LaravelZero\Framework\Commands\Command; | ||
use Symfony\Component\Process\PhpExecutableFinder; | ||
use Symfony\Component\Process\Process; | ||
|
||
class HttpServeCommand extends Command | ||
{ | ||
protected $signature = 'serve:http'; | ||
|
||
protected $description = 'Start simple web server for development'; | ||
|
||
protected $hidden = true; | ||
|
||
protected int $portOffset = 0; | ||
|
||
public function handle() | ||
{ | ||
$this->call('clean'); | ||
|
||
$this->line("<info>Starting development server:</info> http://{$this->host()}:{$this->port()}"); | ||
|
||
$process = $this->startProcess(); | ||
|
||
while ($process->isRunning()) { | ||
usleep(0.5 * 1000000); | ||
} | ||
|
||
$status = $process->getExitCode(); | ||
|
||
if ($status && $this->portOffset++ < 10) { | ||
$this->handle(); | ||
} | ||
|
||
return $status; | ||
} | ||
|
||
protected function startProcess() | ||
{ | ||
$process = new Process($this->serverCommand(), timeout: 0); | ||
|
||
$process->start(function ($type, $data) { | ||
// $this->output->write($data); | ||
}); | ||
|
||
// Stop the server when the user hits Ctrl+C | ||
// to void the port in used error | ||
$this->trap(fn () => [SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2, SIGQUIT], function ($signal) use ($process) { | ||
if ($process->isRunning()) { | ||
$process->stop(10, $signal); | ||
} | ||
|
||
exit; | ||
}); | ||
|
||
return $process; | ||
} | ||
|
||
protected function serverCommand() | ||
{ | ||
return [ | ||
(new PhpExecutableFinder)->find(false), | ||
'-S', | ||
$this->host().':'.$this->port(), | ||
'-t', | ||
'public', | ||
base_path('server.php') | ||
]; | ||
} | ||
|
||
protected function host() | ||
{ | ||
return '127.0.0.1'; | ||
} | ||
|
||
protected function port() | ||
{ | ||
return 6969 + $this->portOffset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace BangNokia\Lina\Commands; | ||
|
||
use BangNokia\Lina\Socket; | ||
use BangNokia\Lina\Watcher; | ||
use Illuminate\Support\Facades\Cache; | ||
use LaravelZero\Framework\Commands\Command; | ||
use Ratchet\Http\HttpServer; | ||
use Ratchet\Server\IoServer; | ||
use Ratchet\WebSocket\WsServer; | ||
use React\EventLoop\Loop; | ||
use React\EventLoop\LoopInterface; | ||
use Symfony\Component\Finder\Finder; | ||
use Ratchet\ConnectionInterface; | ||
use React\Socket\SocketServer as Reactor; | ||
|
||
|
||
class WebsocketServeCommand extends Command | ||
{ | ||
protected $signature = 'serve:ws'; | ||
|
||
protected $description = 'Start websocket server for development'; | ||
|
||
protected $hidden = true; | ||
|
||
protected LoopInterface $loop; | ||
|
||
protected IoServer $server; | ||
|
||
public static int $port = 9696; | ||
|
||
public static int $portOffset = 0; | ||
|
||
public function handle() | ||
{ | ||
$this->loop = Loop::get(); | ||
|
||
$this->loop->futureTick(function () { | ||
$this->line("<info>Starting websocket server:</info> ws://{$this->host()}:{$this->port()}"); | ||
}); | ||
|
||
$this | ||
->startWatcher() | ||
->startServer(); | ||
} | ||
|
||
protected function startWatcher(): static | ||
{ | ||
$dirs = $this->dirs(); | ||
|
||
if (empty($dirs)) { | ||
$this->warn('No directory to watch, please check you are in the correct directory.'); | ||
return $this; | ||
} | ||
|
||
$finder = (new Finder())->files()->in($this->dirs()); | ||
|
||
(new Watcher($this->loop, $finder)) | ||
->startWatching(function () { | ||
$this->info('Changes detected, reloading...'); | ||
collect(Socket::$clients) | ||
->map(function (ConnectionInterface $client) { | ||
$client->send('reload'); | ||
}); | ||
}); | ||
|
||
return $this; | ||
} | ||
|
||
protected function startServer(): static | ||
{ | ||
try { | ||
$this->server = new IoServer( | ||
new HttpServer(new WsServer(new Socket())), | ||
new Reactor("{$this->host()}:{$this->port()}", [], $this->loop), | ||
$this->loop | ||
); | ||
$this->loop->addPeriodicTimer(1, fn() => Cache::put('ws_is_running', true, 5)); | ||
|
||
$this->server->run(); | ||
} catch (\Exception $exception) { | ||
if (static::$portOffset < 10) { | ||
static::$portOffset++; | ||
$this->startServer(); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public static function isRunning(): bool | ||
{ | ||
return Cache::get('ws_is_running', false); | ||
} | ||
|
||
public static function host() | ||
{ | ||
return '127.0.0.1'; | ||
} | ||
|
||
public static function port() | ||
{ | ||
return static::$port + static::$portOffset; | ||
} | ||
|
||
protected function dirs(): array | ||
{ | ||
$currentDir = getcwd(); | ||
|
||
$proposalDirs = [ | ||
$currentDir . '/content', | ||
$currentDir . '/public', | ||
$currentDir . '/resources/views', | ||
]; | ||
|
||
$realDirs = []; | ||
|
||
foreach ($proposalDirs as $dir) { | ||
if (is_dir($dir)) { | ||
$realDirs[] = $dir; | ||
} | ||
} | ||
|
||
return $realDirs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.