2015-12-20 39 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';