PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

[FIXED] Ratchet basic chat application giving error "Failed opening required"

 January 22, 2022     composer-php, php, ratchet, webserver, websocket     No comments   

Issue

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

Solution

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.



Answered By - mattexx
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing