如何纠正棘轮中的 [PHP 致命错误:找不到接口 'Ratchet\MessageComponentInterface'] [英] How to rectify [PHP Fatal error: Interface 'Ratchet\MessageComponentInterface' not found ] in ratchet
问题描述
我正在尝试通过 YouTube 中的教程创建一个基本的 websocket 聊天,但是当我运行时,我在终端中遇到了这个错误
php bin/server.php
<块引用>
致命错误:在第 6 行的/var/www/html/websocket/bin/chat.php 中找不到接口Ratchet\MessageComponentInterface"
我的chat.php代码如下:
clients=new \SplObjectStorage;}公共函数 onOpen(ConnectionInterface $conn){$this->clients->attach($conn);}公共函数 onClose(ConnectionInterface $conn){$this->clients->detach($conn);}公共函数 onMessage(ConnectionInterface $conn,$msg){foreach($this->clients as $client){if($client !==$conn){$client->send($msg);}}}公共函数 onError(ConnectionInterface $conn, \Exception $e){echo "出现以下错误:".$e->getMessage();$conn->close();}}
server.php 的代码:
run();
任何帮助将不胜感激.
在使用 Ratchet\MessageComponentInterface
之前,包含 autoload.php 文件,该文件包含自动加载的所有定义.
在定义命名空间后包含此代码片段:
需要目录名(__DIR__).'/vendor/autoload.php';
I'm trying to create a basic websocket chat from a tutorial in YouTube and I'am facing this error in the terminal when I run
php bin/server.php
Fatal error: Interface 'Ratchet\MessageComponentInterface' not found in /var/www/html/websocket/bin/chat.php on line 6
My code is as follows for 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();
}
}
Code for 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();
Any help would be appreciated.
Include the autoload.php file which has all the definitions for autoloading before using the Ratchet\MessageComponentInterface
.
Include this code snippet just after defining the namespace:
require dirname(__DIR__) . '/vendor/autoload.php';
这篇关于如何纠正棘轮中的 [PHP 致命错误:找不到接口 'Ratchet\MessageComponentInterface']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!