Commit 75bf983b authored by Mat's avatar Mat

rattrape ce qui a été fait sans moi ce matin

parent bdc29ff0
......@@ -31,6 +31,7 @@
let token = '{{ token }}';
let connection = new WebSocket('ws://0.0.0.0:8088/');
let dest;
// ce qui se passe après la connexion (appellé 1 seule fois)
connection.onopen = function(e) {
......@@ -43,6 +44,16 @@
$('#send').click(function() {
// envoyer un nouveau message
let data = JSON.stringify({
'path': '/newMessage',
'token': token,
'message': $('#message').val(),
'dest': dest
});
if (dest != null)
{
connection.send(data);
}
});
};
......@@ -64,6 +75,18 @@
console.log(data);
$('#history').html(data.view);
break;
case 'message_received':
if (data.from == dest)
{
$('#history').html(data.view);
}
else {
alert("Nouveau message de " + data.from);
}
break;
}
......@@ -77,6 +100,8 @@
// annule le comportement par défaut de l'élément
e.preventDefault();
dest = $(this).attr('href');
// envoie les données à la fonction onMessage (chathub), la response ce sera l'historique
connection.send(JSON.stringify({
'path': '/history', //la route
......
{% for message in messages %}
{# @var message \SocketBundle\Entity\Message #}
<div class="{% if message.author.username == author %}right{% else %}left{% endif %}">
<div class="{% if message.author.username == username %}right{% else %}left{% endif %}">
<div class="avatar">
<img src="{{ 'http://0.0.0.0:81/web/assets/img/avatar/' ~ message.author.avatar | default('default.jpg') }}"
alt="avatar" class="avatar">
......
......@@ -140,24 +140,55 @@ class ChatHub implements MessageComponentInterface
// appelle getUser
$username = $this->getUser($token);
// récupère l'historique des messages entre les 2 utilisateurs
$messageRepo = $this->getDoctrine()->getRepository(Message::class);
$messages = $messageRepo->getHistory($username, $dest);
// passe l'historique dans le fragment twig
$twig = $this->container->get('templating');
$fragment = $twig->render('@Socket/Chat/usersHistory.html.twig', array(
'author' => $username,
'messages' => $messages
$this->refreshMessages($username, $dest);
break;
case '/newMessage':
$em = $this->getDoctrine()->getManager();
$userRepo = $this->getDoctrine()->getRepository(User::class);
$dataMessage = $data->message;
$dataDest = $data->dest;
$username = $this->getUser($token);
$author = $userRepo->findOneBy(array(
'username' => $username
));
$receiver = $userRepo->findOneBy(array(
'username' => $dataDest
));
/**
* @var $author User
* @var $receiver User
*/
$message = new Message();
$message->setContent($dataMessage)
->setReceiver($receiver)
->setAuthor($author)
->setDate(new \DateTime())
;
$em->persist($message);
$em->flush();
//maj from messages history
$this->refreshMessages($username, $dataDest);
// retourne le fragment en json, à $from
// on aurait pu mettre $this->clients[$username] à la place de $from
$from->send(json_encode(array(
'action' => 'refresh_messages',
'view' => $fragment
)));
//notification destinataire
$messageRepo = $this->getDoctrine()
->getRepository(Message::class);
$messages = $messageRepo->getHistory($dataDest, $username);
$twig = $this->container->get('templating');
$view = $twig->render("@Socket/Chat/usersHistory.html.twig", [
"messages" => $messages,
"username" => $dataDest,
]);
$this->clients[$dataDest]->send(json_encode([
"action" => "message_received",
"from" => $username,
"view" => $view
]));
break;
}
......@@ -196,6 +227,34 @@ class ChatHub implements MessageComponentInterface
}
}
/**
* @param $username
* @param $dest
* @throws \Twig\Error\Error
*/
private function refreshMessages($username, $dest)
{
// récupère l'historique des messages entre les 2 utilisateurs
$messageRepo = $this->getDoctrine()->getRepository(Message::class);
$messages = $messageRepo->getHistory($username, $dest);
// passe l'historique dans le fragment twig
$twig = $this->container->get('templating');
$fragment = $twig->render('@Socket/Chat/usersHistory.html.twig', array(
'username' => $username,
'messages' => $messages
));
// retourne le fragment en json
$this->clients[$username]->send(json_encode(array(
'action' => 'refresh_messages',
'view' => $fragment
)));
}
/**
* @return \Doctrine\Bundle\DoctrineBundle\Registry|mixed|object
*/
......
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