致命错误:在C:\wamp\www\myRatchetTutorial\src\MyApp\Pusher.php的第6行找不到“Ratchet\Wamp\WampServerInterface”界面。

3

我想运行 Ratchet 应用程序演示,但是出现了如标题中所示的错误。

我的文件结构如下:

/bin/chat-server
/bin/push-server
/src/MyApp/Chat.php
/src/MyApp/Pusher.php
/vendor/ ..
composer.json

这是我的 Pusher 类,我在这里遇到了错误:

<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;

class Pusher implements WampServerInterface {

    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
    }
    public function onOpen(ConnectionInterface $conn) {
    }
    public function onClose(ConnectionInterface $conn) {
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->close();
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
    }


        /**
     * A lookup of all the topics clients have subscribed to
     */
    protected $subscribedTopics = array();

    public function onSubscribe(ConnectionInterface $conn, $topic) {
        $this->subscribedTopics[$topic->getId()] = $topic;
    }

    /**
     * @param string JSON'ified string we'll receive from ZeroMQ
     */
    public function onBlogEntry($entry) {
        $entryData = json_decode($entry, true);

        // If the lookup topic object isn't set there is no one to publish to
        if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
            return;
        }

        $topic = $this->subscribedTopics[$entryData['category']];

        // re-send the data to all the clients subscribed to that category
        $topic->broadcast($entryData);
    }

    /* The rest of our methods were as they were, omitted from docs to save space */
}

我的WampServerInterface在以下位置:

C:\wamp\www\myRatchetTutorial\vendor\cboden\ratchet\src\Ratchet\Wamp\WampServerInterface.php

我尝试重新组织这个文件结构,使其更加清晰明了:

C:\wamp\www\myRatchetTutorial\vendor\Ratchet\Wamp\WampServerInterface.php

但仍然出现相同的错误。

有什么想法可以解决吗?

这是我的composer.json文件:

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*",
        "react/zmq": "0.2.*|0.3.*"
    }
}

Push_server.php(包括autoload文件):


<?php
    require dirname(__DIR__) . '/vendor/autoload.php';

    $loop   = React\EventLoop\Factory::create();
    $pusher = new MyApp\Pusher;

    // Listen for the web server to make a ZeroMQ push after an ajax request
    $context = new React\ZMQ\Context($loop);
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    $pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
    $pull->on('message', array($pusher, 'onBlogEntry'));

    // Set up our WebSocket server for clients wanting real-time updates
    $webSock = new React\Socket\Server($loop);
    $webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
            )
        ),
        $webSock
    );

    $loop->run();

展示如何自动加载文件以及什么是 composer.js - 你是否意味着 composer.json - Marcin Nabiałek
我该如何自动加载文件?我上传了composer.json,这就足够了吗?还是你想看到什么?没错,我指的是composer.json。 - mrow
你确实在某个地方包含了 vendor/autoload.php 文件,是吗? ;) - helmbert
是的,我上传了我的push_server.php文件,它在哪里? - mrow
有什么建议如何修复它吗?问题在文件结构中吗? - mrow
我今天仍然有这个问题...如果我找到了解决方法,我会贡献出来的! - Thanasis
3个回答

0
你应该检查自动加载文件,如果你正在使用Laravel,例如,看一下文件夹:vendor/composer,那里应该有一个名为autoload_classmap的文件,并找到这行代码:
'Ratchet\\Wamp\\WampServerInterface' => $vendorDir . '/cboden/ratchet/src/Ratchet/Wamp/WampServerInterface.php',

如果文件中没有任何关于Ratchet的内容,请在命令行中运行 composer dump-autoload

0
在您的项目根目录下删除composer.lock文件并创建此composer.json文件。
{
    "autoload": {
        "psr-4": {
            "MyApp\\": "src"
        }
    },
    "require": {
        "cboden/ratchet": "^0.4.2",
        "react/zmq": "0.4.0"
    }
}

在您的终端中,cd到您的根目录并输入:
composer install

这将为您获取正确的组件。

您的文件夹应该像这样: bin/push-server.php src/Pusher.php

从项目根文件夹运行“php bin/push-server.php”终端命令。

如果有人发现我的建议有用,请给我评分,因为stackoverflow已经禁止我提问。我会非常感激。

谢谢!


0
你需要在 /src/Pusher.php 文件中实现 Ratchet\Wamp\WampServerInterface 接口的所有方法。
`<?php
    namespace MyApp;
    use Ratchet\ConnectionInterface;
    use Ratchet\Wamp\WampServerInterface;
   class Pusher implements WampServerInterface {
           protected $subscribedTopics = array();

           public function onSubscribe(ConnectionInterface $conn, $topic) {
                   $this->subscribedTopics[$topic->getId()] = $topic;
           }

           public function onUnSubscribe(ConnectionInterface $conn, $topic) {
           }

           public function onOpen(ConnectionInterface $conn) {
           }

           public function onClose(ConnectionInterface $conn) {
           }

           public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
           // In this application if clients send data it's because the user hacked around in console
                   $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
          }

          public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
    // In this application if clients send data it's because the user hacked around in console
                 $conn->close();
          }

          public function onError(ConnectionInterface $conn, \Exception $e) {
          }
          /**
          * @param string JSON'ified string we'll receive from ZeroMQ
          */

         public function onBlogEntry($entry) {
               $entryData = json_decode($entry, true);

        // If the lookup topic object isn't set there is no one to publish to
           if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
        return;
          }

        $topic = $this->subscribedTopics[$entryData['category']];

        // re-send the data to all the clients subscribed to that category
        $topic->broadcast($entryData);
   }

    /* The rest of our methods were as they were, omitted from docs to save space */
    }

?>


1
抱歉,我们只使用英语。请翻译您的回答。 - black blue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接