Answer the question
In order to leave comments, you need to log in
Any idea how to dispatch events in WS ratchet server?
Hello everyone, I wrote a small WS server according to this article
As a result, I got the following console command
Which starts the WS server itself
<?php
namespace App\Command;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use App\Websocket\MessageHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class WebsocketServerCommand extends Command
{
protected static $defaultName = "run:websocket-server";
protected function execute(InputInterface $input, OutputInterface $output)
{
$port = 3001;
$output->writeln("Starting server on port " . $port);
$server = IoServer::factory(
new HttpServer(
new WsServer(
new MessageHandler()
)
),
$port
);
$server->run();
return 0;
}
}
<?php
namespace App\Websocket;
use Exception;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
class MessageHandler implements MessageComponentInterface
{
public function onOpen(ConnectionInterface $conn)
{
}
public function onMessage(ConnectionInterface $from, $msg)
{
}
public function onClose(ConnectionInterface $conn)
{
}
public function onError(ConnectionInterface $conn, Exception $e)
{
}
}
Answer the question
In order to leave comments, you need to log in
Connect to your own web server and send it a system message (do not forget about authorization).
The websocket ratchet server is a 1-threaded application; accordingly, after receiving the message, the method will be executed in the same space where you can store the list of clients. I remember exactly that the ratchet had an array with a list of all connected clients, I'm not sure if it is publicly available directly, but in the worst case, you can collect your own list, available globally.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question