|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BangNokia\Lina\Commands; |
| 4 | + |
| 5 | +use BangNokia\Lina\Socket; |
| 6 | +use BangNokia\Lina\Watcher; |
| 7 | +use Illuminate\Support\Facades\Cache; |
| 8 | +use LaravelZero\Framework\Commands\Command; |
| 9 | +use Ratchet\Http\HttpServer; |
| 10 | +use Ratchet\Server\IoServer; |
| 11 | +use Ratchet\WebSocket\WsServer; |
| 12 | +use React\EventLoop\Loop; |
| 13 | +use React\EventLoop\LoopInterface; |
| 14 | +use Symfony\Component\Finder\Finder; |
| 15 | +use Ratchet\ConnectionInterface; |
| 16 | +use React\Socket\SocketServer as Reactor; |
| 17 | + |
| 18 | + |
| 19 | +class WebsocketServeCommand extends Command |
| 20 | +{ |
| 21 | + protected $signature = 'serve:ws'; |
| 22 | + |
| 23 | + protected $description = 'Start websocket server for development'; |
| 24 | + |
| 25 | + protected $hidden = true; |
| 26 | + |
| 27 | + protected LoopInterface $loop; |
| 28 | + |
| 29 | + protected IoServer $server; |
| 30 | + |
| 31 | + public static int $port = 9696; |
| 32 | + |
| 33 | + public static int $portOffset = 0; |
| 34 | + |
| 35 | + public function handle() |
| 36 | + { |
| 37 | + $this->loop = Loop::get(); |
| 38 | + |
| 39 | + $this->loop->futureTick(function () { |
| 40 | + $this->line("<info>Starting websocket server:</info> ws://{$this->host()}:{$this->port()}"); |
| 41 | + }); |
| 42 | + |
| 43 | + $this |
| 44 | + ->startWatcher() |
| 45 | + ->startServer(); |
| 46 | + } |
| 47 | + |
| 48 | + protected function startWatcher(): static |
| 49 | + { |
| 50 | + $dirs = $this->dirs(); |
| 51 | + |
| 52 | + if (empty($dirs)) { |
| 53 | + $this->warn('No directory to watch, please check you are in the correct directory.'); |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + $finder = (new Finder())->files()->in($this->dirs()); |
| 58 | + |
| 59 | + (new Watcher($this->loop, $finder)) |
| 60 | + ->startWatching(function () { |
| 61 | + $this->info('Changes detected, reloading...'); |
| 62 | + collect(Socket::$clients) |
| 63 | + ->map(function (ConnectionInterface $client) { |
| 64 | + $client->send('reload'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + protected function startServer(): static |
| 72 | + { |
| 73 | + try { |
| 74 | + $this->server = new IoServer( |
| 75 | + new HttpServer(new WsServer(new Socket())), |
| 76 | + new Reactor("{$this->host()}:{$this->port()}", [], $this->loop), |
| 77 | + $this->loop |
| 78 | + ); |
| 79 | + $this->loop->addPeriodicTimer(1, fn() => Cache::put('ws_is_running', true, 5)); |
| 80 | + |
| 81 | + $this->server->run(); |
| 82 | + } catch (\Exception $exception) { |
| 83 | + if (static::$portOffset < 10) { |
| 84 | + static::$portOffset++; |
| 85 | + $this->startServer(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + public static function isRunning(): bool |
| 93 | + { |
| 94 | + return Cache::get('ws_is_running', false); |
| 95 | + } |
| 96 | + |
| 97 | + public static function host() |
| 98 | + { |
| 99 | + return '127.0.0.1'; |
| 100 | + } |
| 101 | + |
| 102 | + public static function port() |
| 103 | + { |
| 104 | + return static::$port + static::$portOffset; |
| 105 | + } |
| 106 | + |
| 107 | + protected function dirs(): array |
| 108 | + { |
| 109 | + $currentDir = getcwd(); |
| 110 | + |
| 111 | + $proposalDirs = [ |
| 112 | + $currentDir . '/content', |
| 113 | + $currentDir . '/public', |
| 114 | + $currentDir . '/resources/views', |
| 115 | + ]; |
| 116 | + |
| 117 | + $realDirs = []; |
| 118 | + |
| 119 | + foreach ($proposalDirs as $dir) { |
| 120 | + if (is_dir($dir)) { |
| 121 | + $realDirs[] = $dir; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return $realDirs; |
| 126 | + } |
| 127 | +} |
0 commit comments