Php Ratchet基本聊天应用程序给出错误“;“需要打开失败”;

Php Ratchet基本聊天应用程序给出错误“;“需要打开失败”;,php,webserver,websocket,composer-php,ratchet,Php,Webserver,Websocket,Composer Php,Ratchet,我正在尝试使用位于的WebSockets的Ratchet库,但在Ubuntu中从命令行运行服务器脚本时遇到一些问题 在成功安装composer和Ratchet之后,我将在上学习基本聊天应用程序的教程,并开始运行它。我的文件结构(websockets是我的项目文件夹)是: kingsconflict websockets chat.php chat-server.php composer.json vendor autoloa

我正在尝试使用位于的WebSockets的Ratchet库,但在Ubuntu中从命令行运行服务器脚本时遇到一些问题

在成功安装composer和Ratchet之后,我将在上学习基本聊天应用程序的教程,并开始运行它。我的文件结构(websockets是我的项目文件夹)是:

kingsconflict
   websockets
      chat.php
      chat-server.php
      composer.json
      vendor
         autoload.php
         (dependecies included by composer for Ratchet)
在第5行的/var/www/kingsconflict/websockets/chat-server.php中键入“sudo php chat server.php”时出现的错误是“php致命错误:require():无法打开所需的'/var/www/kingsconflict/vendor/autoload.php'(include_path='。:/usr/share/php:/usr/share/pear')。它似乎试图打开/var/www/kingsconflict/vendor/autoload.php,但实际路径是/var/www/kingsconflict/websockets/vendor/autoload.php,我不确定它为什么要这样做

chat server.php

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';    // Error here

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

    $server->run();
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}
其他文件的代码与Ratchet教程中所示的相同,只是以防万一,我将在下面发布它们

chat.php

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';    // Error here

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

    $server->run();
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}
autoload.php(没有编辑此内容,但怎么回事)


您的问题在于文件结构。仔细阅读本教程可以发现,聊天类应该位于/src/MyApp/chat.php中,服务器脚本应该位于/bin/chat server.php中。

假设composer.json是

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/Ratchet": "0.3.*"
    }
}
在启动bin/chat-server.php之前,必须使用以下内容更新自动加载文件:

$ composer.phar update

尝试先使用以下命令自动加载文件:

$ composer update

如果仍然不起作用,那么包括行
require'chat.php'
,就在
聊天服务器.php
文件的开头。这对我很有用。

@RaheelHasan之后你有没有找到解决方案?哇,这真的很难弄清楚,它也区分大小写!值得注意的是,如果您只想重新生成自动加载文件,则可以运行
composer dumpautoload
$ composer update