我正在尝试根据 YouTube 中的教程创建基本的 websocket 聊天,但在运行时我在终端中遇到了这个错误
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();
如有任何帮助,我们将不胜感激。
最佳答案
在使用 Ratchet\MessageComponentInterface
之前包含 autoload.php 文件,该文件具有自动加载的所有定义。
在定义命名空间后立即包含此代码片段:
需要目录名(__DIR__) 。 '/vendor/autoload.php';
关于php - 如何纠正 [PHP fatal error : Interface 'Ratchet\MessageComponentInterface' not found ] in ratchet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378161/