I'm working on a WebSocket implementation using Php's Ratchet library in a Drupal Project.
Everything works if I initialize the WebSocket class in the same file as the one where it is defined (WebSocketServer.php).
However, if I try to move the inizialization logic to another file (start_socket.php), which would be nice to keep the project organized, and then import the class WebSocketServer by using namespace and use keywords, I get the following error when executing php start_socket.php:
PHP Fatal error: Uncaught Error: Class 'Drupal\module_name\WebSocketServer' not found in D:\PROYECTOS_Y_EDICION\Programming\project_name\project_name\modules\custom\module_name\src\start_socket.php:15 Stack trace: #0 {main} thrown in D:\PROYECTOS_Y_EDICION\Programming\project_name\project_name\modules\custom\module_name\src\start_socket.php on line 15
Here is the beginning of the WebSocketServer file:
<?php
namespace Drupal\module_name;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once '../../../../vendor/autoload.php';
class WebSocketServer implements MessageComponentInterface {
and here is the start_socket.php file:
<?php
namespace Drupal\module_name;
set_time_limit(0);
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use WebSocketServer;
require_once '../../../../vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(new WsServer(new WebSocketServer())),
8085
);
$server->run();
I have tried importing the class with require_once instead of namespace and use, as follows:
<?php
set_time_limit(0);
require_once '../../../../vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php';
require_once '../../../../vendor/cboden/ratchet/src/Ratchet/Http/HttpServer.php';
require_once '../../../../vendor/cboden/ratchet/src/Ratchet/Http/CloseResponseTrait.php';
require_once '../../../../vendor/cboden/ratchet/src/Ratchet/WebSocket/WsServer.php';
require_once 'WebSocketServer.php';
require_once '../../../../vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(new WsServer(new WebSocketServer())),
8085
);
$server->run();
but that gives me the following error:
PHP Fatal error: Trait 'Ratchet\Http\CloseResponseTrait' not found in D:\PROYECTOS_Y_EDICION\Programming\project_name\project_name\vendor\cboden\ratchet\src\Ratchet\Http\HttpServer.php on line
The directory tree for the Drupal module is the following:
module_name/
-----------src/
--------------start_socket.php
--------------WebSocketServer.php
Any idea of why I'm getting these errors?