【问题标题】:Fail to run websocket server无法运行 websocket 服务器
【发布时间】:2020-02-02 14:19:30
【问题描述】:

我尝试运行我的 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

这是为什么呢?

【问题讨论】:

    标签: php websocket ratchet


    【解决方案1】:

    use-ing 类之后,您是require-ing vendor/autoload.php 文件。

    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 中的拼写错误,您需要在命名空间 指向的路径前加上/告诉作曲家在给定目录下查找文件。

    json 的结果将是:

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

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

    您可以阅读有关 PSR-4 自动加载 herehere 的更多信息。

    【讨论】:

    • @QwertTrewq 请检查编辑并告诉我它是否解决了您的问题。
    • 它在运行composer dump-autload 后没有进行更改。为什么这个命令是必要的?
    • 每次添加/删除依赖项、命名空间甚至 classmaps 时,您都需要运行 composer dump-autoload 命令转储旧的自动加载文件并创建一个包含所有自动加载类的新文件.
    • 你会尝试在同一个根命名空间中创建一个类,然后将它导入到呈现的类中吗?据我所知,这不应该工作,因为 dealspace_websocket 命名空间没有自动加载。
    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 2013-04-03
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多