运行 websocket 服务器失败

Fail to run websocket server

我尝试 运行 我的 websocket 服务器,但由于某些我无法弄清楚的原因,它无法 运行。 我使用了这个教程:http://socketo.me/docs/hello-world

我有这个主文件夹: enter image description here

composer.js:

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

websocket_src 文件夹中,只有 1 个文件,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();
    }
}

最后一个文件,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();
?>

然后,当我在主文件夹中打开 CMD 时,我 运行: php websocket_server.php

这是结果: enter image description here

这是为什么?

您正在 require-ing vendor/autoload.php 文件后 use-ing 类.

require __DIR__ . '/vendor/autoload.php'; 放在文件的顶部,PHP 将能够找到 类。

<?php

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

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

编辑


还有一个关键的部分我一开始没有注意到,就是composer.json中的错字,你需要在namespace所在的路径前加上前缀用 / 指示作曲家在给定目录下查找文件。

结果 json 将是:

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

进行此更改后运行composer dump-autoload

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