Laravel 5.1 : Run Custom Artisan Command in Background

标签 laravel websocket laravel-5.1 laravel-artisan ratchet

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

我尝试使用 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();
}
}

最佳答案

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

php artisan chat_server:serve

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

关于Laravel 5.1 : Run Custom Artisan Command in Background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569162/

相关文章:

Laravel 5.5 预期响应代码 250 但得到代码 "530"

php - Laravel 5.8 中卡在/电子邮件/验证上

php - 请求验证总是使用 Dingo/Api 在 Laravel 上通过

php - 在 Laravel/Eloquent 中查询多个嵌套关系

php - 无法在 laravel 的 Controller 中使用数据库事务(DB::beginTransaction)

laravel - 使用 Traefik、Nginx 和 Laravel 配置 Docker

java - 是什么让这个 android 库成为真正的 websocket 客户端库?

objective-c - 如何在 Objective C 中将 int 转换为 big-endian 32 位数字?

javascript - 带有 wss ://的 WebSocket 操作码 7

php - 将我的 .txt 文件放在哪里以便 PHP 在 Laravel 中读取它们