【问题标题】:Laravel 5.1: Run Custom Artisan Command in BackgroundLaravel 5.1:在后台运行自定义 Artisan 命令
【发布时间】:2016-04-06 18:25:34
【问题描述】:

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

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

编辑:

我的托管服务提供商不允许我通过 ssh 运行 Artisan 命令。

这是自定义 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();
}
}

【问题讨论】:

    标签: laravel websocket laravel-5.1 laravel-artisan ratchet


    【解决方案1】:

    您只需在控制台中使用命令运行它:

    php artisan chat_server:serve
    

    也许您应该确保它始终有效。其中一种方法是使用Supervisor 确保此命令(几乎)一直运行

    【讨论】:

    • 我应该提到我无法通过共享主机帐户中的 ssh 访问“Artisan”命令。
    • 我尝试使用此解决方案:stackoverflow.com/questions/32216857/…。但我无法让它工作。
    • 恐怕不是一件容易的事。从理论上讲,您可以在 cron(假设您有 cron)中运行例如 1 分钟 $server-&gt;run();$server-&gt;stop(); 但它不会很可靠,因此您应该考虑转移到 VPS 托管,您可以在 cron 中运行命令并制作确保它们一直在运行
    • 谢谢。我正在考虑使用有 1 年免费计划的 AWS。
    猜你喜欢
    • 2015-11-19
    • 2015-12-07
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2018-06-07
    • 2018-07-01
    • 2015-11-13
    相关资源
    最近更新 更多