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.
namespace
first and set that, then used theuse
? php.net/manual/en/language.namespaces.importing.php I don't really use namespace all that much but the manual appears to do the namespace first, then theuse
. – Rasclattnamespace Ratchet;
? Did you include the file that contains theRatchet
library? – Rasclattnamespace MyApp;
– Rasclatt