-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (33 loc) · 778 Bytes
/
script.js
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
$(() => {
const socket = io();
socket.on('message', addMessage);
$('#send').click(() => {
const name = $('#name').val().trim();
const message = $('#message').val().trim();
if (name == "" || message == "")
alert("empty fields are not allowed");
else {
sendMessage({
name: name,
text: message
});
}
$('#name').val("");
$('#message').val("");
});
getMessages();
});
function addMessage(message) {
$('#messages').prepend(`
<h4> ${message.name} </h4>
<p> ${message.text} </p>
`);
}
function getMessages() {
$.get('http://localhost:3000/messages', (data) => {
data.forEach(addMessage);
});
}
function sendMessage(message) {
$.post('http://localhost:3000/messages', message);
}