Commit eca08127 authored by Mat's avatar Mat

envoie le message à tous les utilisateurs

parent 090f86d6
{% extends "base.html.twig" %}
{% block layout %}
Hello World!
<section style="margin: 2em;">
<h5>
Hello World!
</h5>
<input id="message" type="text">
<button id="send">Envoyer</button>
</section>
{% endblock %}
{% block javascripts %}
......@@ -16,7 +25,8 @@
connection.onopen = function(e)
{
connection.send("hello");
let message = $('#message').val();
connection.send(message);
};
connection.onmessage = function(e)
......
......@@ -19,9 +19,21 @@ class ChatHub implements MessageComponentInterface
* et hop, il ajoute tout ce qui doit être implémenté
*/
/**
* @var ContainerInterface
*/
private $container;
/**
* @var ConnectionInterface
*/
private $clients;
/**
* ChatHub constructor.
*
* @param ContainerInterface $_container
*/
public function __construct(ContainerInterface $_container)
{
$this->container = $_container;
......@@ -37,14 +49,17 @@ class ChatHub implements MessageComponentInterface
*/
function onOpen(ConnectionInterface $conn)
{
// TODO: Implement onOpen() method.
echo "Un utilisateur s'est connecté\n";
$this->clients[] = $conn;
$response = json_encode([
'type' => 'msg_server',
'author' => 'server',
'content' => "Bienvenue sur notre Web Socket Server",
'timestamp' => (new \DateTime())->getTimestamp()
]);
$conn->send($response);
}
......@@ -83,6 +98,16 @@ class ChatHub implements MessageComponentInterface
*/
function onMessage(ConnectionInterface $from, $msg)
{
// TODO: Implement onMessage() method.
echo "Le message ". $msg ." a été reçu\n";
}}
\ No newline at end of file
foreach ($this->clients as $client)
{
$response = json_encode([
'type' => 'msg_from_user',
'author' => "",
'content' => $msg,
'timestamp' => (new \DateTime())->getTimestamp()
]);
$client->send($response);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment