hhxsv5 / laravel-s

LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`App\Console\Commands`下的创建的自定义命令中调用`app('swoole')`显示`Target class [swoole] does not exist`.

MarsTify opened this issue · comments

commented
  1. software version

    Software Version
    PHP 8.1.15
    Swoole laravel-s 3.7.0
    Lumen 8.3.1
  2. Detail description about this issue(error/log)

    如题所描述的: 命名空间App\Console\Commands下的创建的自定义命令中调用app('swoole')显示Target class [swoole] does not exist; 而在控制器中却可以正常调用.

  3. Some reproducible code blocks and steps

    • 自定义命令:
    namespace App\Console\Commands;
    
    use App\Jobs\WebsocketDisconnectionJob;
    use Carbon\Carbon;
    use Illuminate\Console\Command;
    
    class WebSocketMonitor extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'ws-monitor';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'websocket available connections monitor';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            try {
                // 检测 ws连接池 可用链接
                $availableConnections = collect(app('swoole')->connections)
                    ->filter(function ($id) {
                        return app('swoole')->isEstablished($id);
                    });
                if ($availableConnections->count() == 0) {
                    // 通报
                    dispatch(new WebsocketDisconnectionJob());
    
                    $now = Carbon::now();
                    $this->info("[$now] ws-monitor websocket disconnection report!");
                }
                return true;
            } catch (\Exception $e) {
                $time = Carbon::now();
                $this->error("[{$time}] ws-monitor error: {$e->getMessage()}");
                return false;
            }
        }
    }

image

commented

明白了, 感谢回复!