【问题标题】:How to rectify [PHP Fatal error: Interface 'Ratchet\MessageComponentInterface' not found ] in ratchet如何纠正棘轮中的[PHP致命错误:未找到接口'Ratchet\MessageComponentInterface']
【发布时间】:2015-12-20 05:04:54
【问题描述】:

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

php bin/server.php

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

我的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();

任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过先声明namespace 并设置它,然后使用usephp.net/manual/en/language.namespaces.importing.php 我并没有真正使用命名空间,但手册似乎首先使用命名空间,然后是 use
  • 做得好还是不行。
  • 你做了namespace Ratchet;?您是否包含了包含 Ratchet 库的文件?
  • 是的,我做了,让我给你看代码@Rasclatt
  • 你的命名空间在哪里?注意第二个片段:socketo.me/docs/hello-world 它说namespace MyApp;

标签: php websocket ratchet


【解决方案1】:

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

在定义命名空间后包含这段代码sn-p:

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

【讨论】:

    【解决方案2】:

    转到 composer.json 并更改

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

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

    并打开命令升级与管理更新作曲家喜欢

    composer update
    

    你应该在 composer.json 所在的同一个文件夹目录中

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2019-05-06
      • 2015-08-23
      • 1970-01-01
      相关资源
      最近更新 更多