Search code examples
phpwebsocketratchet

Fail to run websocket server


I try to run my websocket server but it fails to run for some reason I cant figure out. I used this tutorial: http://socketo.me/docs/hello-world

I have this main folder: enter image description here

composer.js:

{
    "autoload": {
        "psr-4": {
            "dealspace_websocket\\": "websocket_src"
        }
    },
    "require": {
        "cboden/ratchet": "^0.4.2"
    }
}

Inside websocket_src folder, there is ONLY 1 file, Chat.php:

<?php
namespace dealspace_websocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once __DIR__ . "/../Model/DBConnection.php";

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $JSonMsg = json_decode($msg);
        if($JSonMsg['type'] === 'updownvote') {
            $connection = new DBConnection();
            $allowUpdate = false;
            try {
                $sql = 'UPDATE `deals`
                        SET `votes_counter` = :vc
                        WHERE `id` = :id';
                $stmt = $connection->dbh->prepare($sql);
                $allowUpdate = $stmt->execute(array(
                    'vc' => $JSonMsg['data']['votes'],
                    'id' => $JSonMsg['data']['dealid']
                ));
                if($allowUpdate !== false) {
                    $dataOBJ->dealid = $JSonMsg['data']['dealid'];
                    $dataOBJ->votes = $JSonMsg['data']['votes'];
                    $returnMsg->type = 'updownvote';
                    $returnMsg->date = $dataOBJ;
                    foreach($this->clients as $client) {
                        $client->send(json_encode($returnMsg));
                    }
                }
            } catch(PDOException $e) {}
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        $conn->close();
    }
}

And the last file, websocket_server.php:

<?php
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use dealspace_websocket\Chat;

    require __DIR__ . '/vendor/autoload.php';

    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ),
        8080
    );

    $server->run();
?>

Then, when I open the CMD in the main folder, I run: php websocket_server.php

This is the result: enter image description here

Why is that?


Solution

  • You are require-ing the vendor/autoload.php file after use-ing the classes.

    Put the require __DIR__ . '/vendor/autoload.php'; at the top of the file and PHP will be able to find the classes.

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use dealspace_websocket\Chat;
    

    EDIT


    There is also one crucial part that i did not notice at first, which is the typo in composer.json, you need to prefix the path to which the namespace points with a / to tell composer to look for files under the given directory.

    The resulted json would be:

    "psr-4": {
        "dealspace_websocket\\": "websocket_src/"
    }
    

    After making this change run composer dump-autoload

    You can read more about PSR-4 autoloading here and here.