Class 'MyChat\Chat' not found in C:\wamp\www\bin\chat-server.php [duplicate]
Asked Answered
T

1

1

I am trying to implement the basic chat application from http://socketo.me/docs/hello-world, however I keep getting this error. I tried to move files around, but with no success, but I am quite sure that I don't put the files in the right place. I'm completly new to composer and websockets and psr-0 and I still have a lot to learn about PHP. Here are my path tree and my sources:

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

chat-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"
        }
    }
}

My php.exe is in C:\wamp\bin\php\php5.4.12 . I would be really thankful for a suggestion, I can't really spot where am I mistaken.

Topi answered 11/8, 2013 at 13:59 Comment(9)
Have you added MyChatBundle to app/AppKernel.php ?Snowfield
@Snowfield I have no "app" directory, so obviously no AppKernel.php, nowhere in the tutorial is suggested anything about it, neither does the repository of the project on github have any "app" directory. I have posted the complete tree of my project (apart from the vendor directory with the dependencies)Topi
Can you post the exact command you are using to run the server script? Are you running from www, or bin?Snowfield
Check your local network routing firewall,it can be band ports like 8080.Or your client machine diffrent from server machine check both side.Mohandas
@Snowfield php bin/chat-server.php is the command I am using, as specified in the docs.Topi
@ShahrokhMoghimi The file chat server is obviously found, because an error is thrown ("Class 'MyChat\Chat' not found in C:\wamp\www\bin\chat-server.php on line 5")-where I create an instance of the Chat class. Even if the port would be banned, there should be an error related to that rather than "class not found". Again, I am quite sure that the file locations are not okay.Topi
I copy pasted your scripts with the same path tree and ran them without a problem (other than line 3 in Chat.php which I deleted, but I doubt that's your problem). Honestly, trying to do this stuff on Windows nearly drove me insane, I'd advise doing this all on Linux as it's 100x easier and cleaner.Jew
@Jew I have some problems trying to install PhpStorm on my Ubuntu and I really love PhpStorm, I wouldn't really leave it. It still gives me class not found. Thanks anyway ;) I've recently started using linux, but not yet for serious app development, until I really get used to itTopi
Well I'd feel bad if I didn't tell you that you sound like me a few weeks ago, running servers and doing everything on Windows and barely scraping Linux because I'd never really used it. But let me tell you, once you make a full leap and put everything to Linux, you will never go back. I can guarantee you that learning Ubuntu and getting a solution to your PhpStorm install problems on Ubuntu will be a hell of a lot easier than trying to get this stuff to work right on Windows. Just my two cents. Wish you the best of luck, Ratchet is great once you figure it out.Jew
L
0

This is a little late but it looks like you are using composer, so you may need to run that installer?

From your directory try running each of these and see if that helps:

./composer.phar install --dev
./composer.phar update
Lamia answered 6/9, 2013 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.