11
votes

I'm trying out the Ratchet library to use WebSockets located at http://socketo.me/ but am experiencing some problems when running the server script from the command line in Ubuntu.

After successfully installing composer and Ratchet I'm following the tutorial for a basic chat application at http://socketo.me/docs/hello-world and I'm at the Running It step. My file structure, with websockets being my project folder, is:

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

The error I get when typing "sudo php chat-server.php" is "PHP Fatal error: require(): Failed opening required '/var/www/kingsconflict/vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/kingsconflict/websockets/chat-server.php on line 5". It seems like it's trying to open /var/www/kingsconflict/vendor/autoload.php but the actual path is /var/www/kingsconflict/websockets/vendor/autoload.php, and I'm not sure why it's doing this.

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

I tried subbing the errored line with the line below, and I stop getting the error but I get a new error "PHP Fatal error: Class 'MyApp\Chat' not found" which leads me to believe this fix isn't right.

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

The code for the other files are the same as shown in the Ratchet tutorial, but just in case I'll post them below

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

composer.json

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

autoload.php (Didn't edit this but what the hell)

<?php

// autoload.php generated by Composer

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

return ComposerAutoloaderInit0964ef3a5e66723368300f04c3206ca1::getLoader();
3

3 Answers

16
votes

Assuming that your composer.json is

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

before launching the bin/chat-server.php you have to update the autoload files with:

$ composer.phar update
9
votes

Your problem is your file structure. Careful reading of the tutorial reveals that your chat class should be in /src/MyApp/Chat.php, and your server script should be in /bin/chat-server.php.

1
votes

Try autoloading the files first with:

$ composer update

If it still doesn't work then include the line require 'chat.php';, just at the beginning of the chat-server.php file. It worked for me.