如何在棘轮中纠正 [PHP 致命错误:找不到接口"棘轮\消息组件接口"]



我正试图从YouTube上的教程中创建一个基本的websocket聊天,当我运行时,我在终端中遇到了这个错误

php bin/server.php

致命错误:在第6行的/var/www/html/websocket/bin/chat.php中找不到接口"Ratchet\MessageComponentInterface"

我的chat.hp:代码如下

<?php
namespace MyApp;
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
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 RatchetServerIoServer;
use RatchetHttpHttpServer;
use RatchetWebSocketWsServer;
$server=IoServer::factory(new HttpServer(new WsServer (new chat())) , 8080);
$server->run();

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

包括autoload.php文件,该文件具有使用RatchetMessageComponentInterface之前自动加载的所有定义。

在定义名称空间之后包含以下代码片段:

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

转到composer.json并更改

{
    "require": {
        "cboden/ratchet": "^0.4"
    }
}

{
    "autoload": {
        "psr-4": {
            "MyApp\": "src"
        }
    },
    "require": {
        "cboden/ratchet": "^0.4"
    }
}

和开放命令促进与管理更新作曲家像

composer update

您应该位于上composer.json所在的同一文件夹目录

最新更新