I faced this "Interface 'Ratchet\MessageComponentInterface' not found" problem - php

I want to use Ratchet web socket in my project and when i tray to connect it i face this problem and i tray to update and dump the composer but it dose not work ! any one who can help me please ?
here is my composer.json file
{
"autoload": {
"psr-4": {
"MyChatApp\\": "src"
}
},
"require": {
"cboden/ratchet": "^0.4.1"
}
}
my server.php file
<?php
require 'core/Chat.php';
require __DIR__ . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use MyApp\Chat;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8081
);$server->run();
my chat.php file
<?php
namespace MyChatApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{ protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg)
{
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n",
$from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
//The sender is not the receiver, send to other clients
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
// The connection is closed, remove from connection list
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
and the error is "Fatal error: Interface 'Ratchet\MessageComponentInterface' not found in C"

Related

Can't access SESSION vairable in class when using Rachet Library

I developed a chat website main page is index.php and I assigned id variable in session using get variable (index.php?id=123) but when I want to use this session in chat.php which has code like this :
<?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) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
session_start();
print_r($_SESSION); // <---- session variable
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client => $val) {
if ($from !== $client) {
echo $val;
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
?>
but session variable is empty
how can I use session in class
You can ask for any resource

WebSocket PHP handles each message separately

I use Ratchet library to create websocket on php and I set a timeout of 10 seconds to return a message to the client, then I see that it will wait for the first message to be processed before processing the next message, how to make each message it will process always without waiting for the previous message
socket.php
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Socket implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$time = date("H:i:s");
sleep(10);
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s"' . "\n"
, $from->resourceId, $msg);
$from->send("Server Reply: ".$time);
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
run.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Socket;
require 'vendor/autoload.php';
require "socket.php";
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket()
)
),
8765
);
$server->run();
Image

how to trigger onMessage function from server-side on ratchet websocket?

in my project , i want to send data to some clients automatically , without receiving any request from clients.but i cant access to clients from out side of MessageComponentInterface class object. i prefer to tell to MessageComponentInterface class ; send to alive clients a message. so i need to trigger onMessage function from server-side , how can i do it ?
here is my WebSocketCon class :
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class WebSocketCon implements MessageComponentInterface {
protected $clients;
public $users;
public function __construct() {
$this->clients = new \SplObjectStorage;
$this->users = [];
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$data = json_decode($msg);
if($data->command=="subscribe"){
$this->users[(int)$data->uid] = $from;
echo "New subscribe! ({$data->uid})\n";
}
}
public function sendMessageToAll($msg){
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function sendMessageTo($idusers,$msg){
foreach ($idusers as $idu) {
$idu = (int) $idu;
if(array_key_exists($idu,$this->users)){
$this->users[$idu]->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
if (($key = array_search($conn, $this->users)) !== false) {
unset($this->users[$key]);
}
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
and here is my cmd.php :
<?php
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require 'classes/WebSocketCon.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new WebSocketCon()
)
),
8081
);
$server->run();
?>

how to send data to some clients in Ratchet websocket from out of MessageComponentInterface?

how can i get a live object of MessageComponentInterface class from another place in my project to sent a notification to some clients ? i need to call sendMessageTo function of WebSocketCon class to sent data to some clients , but i cannot access to connected clients.
here is my WebSocketCon class :
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class WebSocketCon implements MessageComponentInterface {
protected $clients;
public $users;
public function __construct() {
$this->clients = new \SplObjectStorage;
$this->users = [];
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$data = json_decode($msg);
if($data->command=="subscribe"){
$this->users[(int)$data->uid] = $from;
echo "New subscribe! ({$data->uid})\n";
}
}
public function sendMessageToAll($msg){
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function sendMessageTo($idusers,$msg){
foreach ($idusers as $idu) {
$idu = (int) $idu;
if(array_key_exists($idu,$this->users)){
$this->users[$idu]->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
if (($key = array_search($conn, $this->users)) !== false) {
unset($this->users[$key]);
}
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
and here is my cmd.php :
<?php
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require 'classes/WebSocketCon.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new WebSocketCon()
)
),
8081
);
$server->run();
?>

Creating websocket chat rooms

I have a working Ratchet chat socket server, however everyone who connects has the ability to chat with everyone. How can I implement rooms in code, I want users to be able to chat in rooms that arent visible to everyone connected on the server
ChatServer.php
<?php
require_once 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
echo "Hello";
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
echo $msg;
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
If any additional code is needed or if the question is unclear please let me know

Categories