php - 如何纠正 [PHP fatal error : Interface 'Ratchet\MessageComponentInterface' not found ] in ratchet

标签 php websocket ratchet

我正在尝试根据 YouTube 中的教程创建基本的 websocket 聊天,但在运行时我在终端中遇到了这个错误

php bin/server.php

Fatal error: Interface 'Ratchet\MessageComponentInterface' not found in /var/www/html/websocket/bin/chat.php on line 6

我的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)
            {
                $this->clients->attach($conn);
            }

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

        public function onMessage(ConnectionInterface $conn,$msg)
            {
                foreach($this->clients as $client){
                    if($client !==$conn){
                        $client->send($msg);
                    }
                }
            }

        public function onError(ConnectionInterface $conn, \Exception $e)
            {
                echo "the following error occured: ".$e->getMessage();
                 $conn->close();
            }
    }

server.php 的代码:

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

$server=IoServer::factory(new HttpServer(new WsServer (new chat())) , 8080);
$server->run();

如有任何帮助,我们将不胜感激。

最佳答案

在使用 Ratchet\MessageComponentInterface 之前包含 autoload.php 文件,该文件具有自动加载的所有定义。

在定义命名空间后立即包含此代码片段:

需要目录名(__DIR__) 。 '/vendor/autoload.php';

关于php - 如何纠正 [PHP fatal error : Interface 'Ratchet\MessageComponentInterface' not found ] in ratchet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378161/

相关文章:

php - 在 PHP 中打开 OSX 文件包的推荐方法是什么?

php - 从 PHP 调用 Jquery AJAX 后强制重定向

html - websocket 不断断开连接

php - 在自己的可重用库中提供依赖注入(inject)

php 数组重新整理、合并和排序

python - 用于实时数据的 websocket vs REST API?

reactjs - 在 React 应用程序中通过 laravel-echo 创建和加入 websocket

laravel - 使用 Laravel-Websockets 向特定用户发送消息(一对一聊天)

php - 带有 Ratchet 的 Redis 发布订阅

php - Doctrine\ORM\EntityManager,在类 MessageComponentInterface 中未指定