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