2015-12-20 83 views
2

我试图创建的YouTube和我'在终端面对这个错误,当我运行一个教程基本的WebSocket聊天如何纠正[PHP致命错误:接口“棘轮 MessageComponentInterface”未找到在棘轮

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

任何帮助,将不胜感激。

+0

您是否尝试过声明'namespace'第一和设定,然后用'use'? http://php.net/manual/en/language.namespaces.importing.php我并没有真正使用命名空间,但手册似乎首先执行命名空间,然后使用'use'。 – Rasclatt

+0

做得还是不错。 – shan2batman

+0

你做了命名空间棘轮;'?你有包含'Ratchet'库的文件吗? – Rasclatt

回答

1

包括已经对自动加载的所有定义使用Ratchet\MessageComponentInterface前autoload.php文件。

包含此代码段只是定义命名空间之后:

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