ninesurvey/src/ninesurvey-1.0/src/Command/WebsocketServerCommand.php

53 lines
1.5 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
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;
use Symfony\Component\Console\Input\InputOption;
class WebsocketServerCommand extends Command
{
private $container;
private $em;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:Websocket')
->setDescription('Lauch Websocket server')
->setHelp('Lauch Websocket server')
->addOption('name',null,InputOption::VALUE_REQUIRED,'Websocket server name',null)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$port = $this->container->getParameter('wssport');
$output->writeln("Starting server on port " . $port);
$server = IoServer::factory(
new HttpServer(
new WsServer(
new MessageHandler($this->container,$this->em)
)
),
$port
);
$server->run();
return 0;
}
}