2016-01-02 99 views
1

我正在使用Ratchet package开发聊天应用程序。在教程的帮助下,我编写了一个自定义的工匠命令来启动WebSocket服务器。我需要在后台运行这个Artisan命令,它应该始终运行。我该怎么做?Laravel 5.1:在后台运行自定义工匠命令

我尝试使用Artisan Facade的Artisan::queue and Artisan::call。但是,由于我的自定义命令无限期地运行(很长一段时间),它不起作用。

编辑:

我的托管服务提供商是不是让我跑工匠命令通过ssh。

下面是自定义的Artisan命令代码:

<?php 

namespace App\Console\Commands; 

use Illuminate\Console\Command; 

use Ratchet\Http\HttpServer; 
use Ratchet\Server\IoServer; 
use Ratchet\WebSocket\WsServer; 
use App\Classes\Socket\ChatSocket; 
use App\Classes\Socket\Base\BaseSocket; 

class ChatServer extends Command 
{ 
/** 
* The name and signature of the console command. 
* 
* @var string 
*/ 
protected $signature = 'chat_server:serve'; 

/** 
* The console command description. 
* 
* @var string 
*/ 
protected $description = 'Command description'; 

/** 
* Create a new command instance. 
* 
* @return void 
*/ 
public function __construct() 
{ 
    parent::__construct(); 
} 

/** 
* Execute the console command. 
* 
* @return mixed 
*/ 
public function handle() 
{ 
    $this->info("Start server"); 

    $server = IoServer::factory(
     new HttpServer(
      new WsServer(
       new ChatSocket() 
      ) 
     ), 
     8080 
    ); 

    $server->run(); 
} 
} 

回答

0

您应该简单地在控制台命令来运行它:

php artisan chat_server:serve 

,也许你应该确保它会一直工作。其中一种方法是使用Supervisor来确保此命令将始终运行(几乎)

+0

我应该提到,我无法通过共享托管帐户中的ssh访问'Artisan'命令。 – Raghav

+0

我尝试使用此解决方案:http://stackoverflow.com/questions/32216857/execute-laravel-symfony-artisan-command-in-background。但我无法得到它的工作。 – Raghav

+0

恐怕不能轻易完成。理论上你可以在crone(假设你有cron)中运行例如1分钟的'$ server-> run();'和'$ server-> stop();'但它不会很可靠,所以你应该考虑转移到VPS主机,您可以在cron中运行命令并确保它们始终运行 –