在C:\ wamp \ www \ bin \ chat-server.php中找不到“MyChat \ Chat”类

时间:2013-08-11 13:59:28

标签: php websocket chat phpwebsocket ratchet

我正在尝试从http://socketo.me/docs/hello-world实现基本聊天应用程序,但是我一直收到此错误。我试图移动文件,但没有成功,但我很确定我没有把文件放在正确的位置。我完全是作曲家和websockets以及psr-0的新手,我还有很多东西需要学习PHP。这是我的路径树和我的来源:

C:\wamp\www\
        bin
           chat-server.php
        src
            MyChat
                Chat.php
        vendor
           {dependencies}+autoload.php
        composer.json
        composer.phar
        composer.lock

Chat.php

<?php
namespace MyChat;
require dirname(__DIR__) . '\vendor\autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
    protected $clients;
    function __construct()
    {
        $this->clients=new \SplObjectStorage();
    }
    function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})\n";
    }
    function onClose(ConnectionInterface $conn)
    {
        echo "Connection closed: {$conn->resourceId} \n";
        $this->clients->detach($conn);
    }
    function onError(ConnectionInterface $conn, \Exception $e)
    {
        echo "An error has occured: {$e->getMessage()}. Closing connection... \n";
        $conn->close();
    }
    function onMessage(ConnectionInterface $from, $msg)
    {
        $receivers=count($this->clients)-1;
        foreach($this->clients as $client)
        {
            if($client!=$from)
            {
                $client->send($msg);
            }
        }
    }
}

聊天server.php

<?php
require dirname(__DIR__) . '\vendor\autoload.php';
use Ratchet\Server\IoServer;
use MyChat\Chat;
$server= IoServer::factory (new Chat() ,8080,'0.0.0.0');//0.0.0.0 is default, means accept all connections
$server->run();

composer.json

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

我的php.exe位于C:\ wamp \ bin \ php \ php5.4.12。我真的很感谢你的建议,我无法确定我在哪里弄错了。

1 个答案:

答案 0 :(得分:0)

这有点晚了,但看起来你正在使用composer,所以你可能需要运行该安装程序?

从您的目录中尝试运行其中的每一项并查看是否有帮助:

./composer.phar install --dev
./composer.phar update