11

我正在尝试使用 Ratchet 库来使用位于http://socketo.me/的 WebSocket,但是在 Ubuntu 中从命令行运行服务器脚本时遇到了一些问题。

成功安装 composer 和 Ratchet 后,我​​将按照http://socketo.me/docs/hello-world上的基本聊天应用程序教程进行操作,并且正在运行它步骤。我的文件结构,websockets 是我的项目文件夹,是:

kingsconflict
   websockets
      chat.php
      chat-server.php
      composer.json
      vendor
         autoload.php
         (dependecies included by composer for Ratchet)

输入“sudo php chat-server.php”时出现的错误是“PHP 致命错误:require(): Failed opening required '/var/www/kingsconflict/vendor/autoload.php' (include_path='.:/usr /share/php:/usr/share/pear') 在 /var/www/kingsconflict/websockets/chat-server.php 第 5 行"。似乎它正在尝试打开/var/www/kingsconflict/vendor/autoload.php,但实际路径是/var/www/kingsconflict/websockets/vendor/autoload.php,我不确定它为什么这样做。

聊天服务器.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 致命错误:找不到类 'MyApp\Chat'”,这让我相信这个修复是不正确的。

require ('./vendor/autoload.php');

其他文件的代码与 Ratchet 教程中所示的相同,但以防万一我将它们发布在下面

聊天.php

<?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();
    }
}

作曲家.json

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/Ratchet": "0.2.*"
    }
}

autoload.php(没有编辑这个但到底是什么)

<?php

// autoload.php generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit0964ef3a5e66723368300f04c3206ca1::getLoader();
4

3 回答 3

16

假设您的 composer.json 是

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/Ratchet": "0.3.*"
    }
}

在启动 bin/chat-server.php 之前,您必须使用以下命令更新自动加载文件:

$ composer.phar update
于 2013-11-08T09:17:14.010 回答
10

你的问题是你的文件结构。仔细阅读教程会发现你的聊天类应该在 /src/MyApp/Chat.php 中,而你的服务器脚本应该在 /bin/chat-server.php 中。

于 2013-06-04T18:15:34.493 回答
1

首先尝试自动加载文件:

$ composer update

如果它仍然不起作用require 'chat.php';,则在文件开头包含该行chat-server.php。它对我有用。

于 2017-04-07T17:01:50.230 回答