-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html.twig
49 lines (40 loc) · 1.25 KB
/
index.html.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{% extends 'base.html.twig' %}
{% block title %}Chat{% endblock %}
{% block body %}
<h1>Bonjour {{ app.user.username }},</h1>
<div id="messages">
{% for message in messages %}
<p><strong>{{ message.user.username }}</strong> : {{ message.message }}. <i>{{ message.created|date('d/m/Y
H:i')
}}</i></p>
{% endfor %}
</div>
<div id="users"></div>
<div>
<textarea id="texte"></textarea>
<button id="btnEnvoyer">Envoyer</button>
</div>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script>
const u = new URL('http://localhost:3000/hub');
u.searchParams.append('topic', 'http://chatlp.com/message');
const es = new EventSource(u);
es.onmessage = e => {
console.log('received')
var $data = JSON.parse(e.data);
var $html ="<p><strong>"+$data.username+"</strong> : "+$data.message+". <i>"+$data.created+"</i></p>";
$('#messages').append($html);
}
$(document).on('click', '#btnEnvoyer', function () {
$.ajax({
url: "{{ path('post_chat') }}",
data: {
texte: $('#texte').val()
},
method: 'POST'
})
})
</script>
{% endblock %}