Commit eca08127 authored by Mat's avatar Mat

envoie le message à tous les utilisateurs

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