php - 如何让 Ratchet WebSocket 保持事件状态?

标签 php websocket real-time ratchet phpwebsocket

我的本地主机上有一个 Ratchet(PHP 实时类)项目。当我想使用和测试这个项目时,
我运行 CMD 并使用以下命令:C:\wamp\www\ChatApp> php cmd.php (cmd.php 是 websocket 启动器)。
当我按下 Ctrl + C或者退出 cmd,这个 websocket 就死了。

但是,当我将项目转移到主主机以激活此 websocket 时,我该怎么办?

cmd.php:

require 'vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require 'Chat.php';

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new \ChatApp\Chat()
        )
    ), 8080
);

$server->run();


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

$server->run();

Chat.php(类):
namespace ChatApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage();
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
        echo 'Someone Connected'.PHP_EOL;
    }

    public function onMessage(ConnectionInterface $from,$msg) {
        foreach ($this->clients as $client) {
            if ($from !== $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn){
        $this->clients->detach($conn);
        echo 'Someone Has Disconnected'.PHP_EOL;
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An Error Has Occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

最佳答案

您可以在 linux 中使用 (nohup) 命令。在您的 server.php 根目录中编写此代码
nohup php server.php

关于php - 如何让 Ratchet WebSocket 保持事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489377/

相关文章:

php - 使用 Slim Framework 创建 OR 功能

session - Websocket - httpSession 返回 null

php - 有人可以解释如何在将 html 输入值写入 .txt 文件的 php 脚本之后重定向 url

ruby-on-rails - 在Rails应用中进行长轮询

go - WriteMessage 上的 Websocket 失败

linux - Linux下如何高效运行短小的异步任务?

java - 在Java中有什么方法可以共享内存位置来交换一些实时小数据包?

asp.net - 如何分析 ASP.NET/IIS 管道

php - 在“我的帐户”>“在 WooCommerce 中编辑帐户”上添加带有验证的单选按钮

php - 在 codeigniter 中通过查询转义订单