-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
59 lines (46 loc) · 2.03 KB
/
index.html
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
50
51
52
53
54
55
56
57
58
59
<!Doctype html>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<script src="/socket.io/socket.io.js"></script>
<div class="jumbotron">
<div class="container">
<h1 class="display-4">Send Message</h1>
<p class="lead">This is a example for the jumbotron</p>
<form>
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group mt-4">
<textarea type="text" class="form-control" id="message" placeholder="Message"></textarea>
</div>
<button id="send" type="submit" class="btn btn-primary mt-3">Submit</button>
</form>
<div id="messages" class="mt-5">
</div>
</div>
</div>
<script>
var socket = io()
$(() => {
$("#send").click((event) => {
event.preventDefault();
var message = { name: $("#name").val(), message: $("#message").val()};
postMessages(message);
// addMessages({ name: 'Madhu', message: 'Hello' });
});
getMessages();
})
socket.on('message', addMessage)
function addMessage(message) {
$("#messages").append(`<h4> ${message.name} </h4> <p> ${message.message} </p>`)
}
function getMessages() {
$.get('http://localhost:3000/messages', (data) => {
data.forEach(addMessage);
})
}
function postMessages(message) {
$.post('http://localhost:3000/messages', message)
}
</script>