<?php
namespace App\Chat;
use App\Chat\Chat;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ServerWebSocket extends ContainerAwareCommand
{
/**
* Configure a new Command Line
*/
protected function configure()
{
$this
->setName('Project:websocket:server')
->setDescription('Start the websocket server.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream\n\n");
$counter = rand(1, 10);
while (1) {
// Chaque seconde, envoi d’un évènement "ping".
echo "event: ping\n";
$curDate = date(DATE_ISO8601);
echo 'data: {"time": "' . $curDate . '"}';
// Paire de sauts de ligne
echo "\n\n";
// Envoie un message simple à des intervalles aléatoires.
$counter--;
if (!$counter) {
echo 'data: This is a message at time ' . $curDate . "\n\n";
$counter = rand(1, 10);
}
ob_end_flush();
flush();
sleep(1);
}
}
}