-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
85 lines (78 loc) · 2.75 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Chat Room</title>
<!-- Compiled and minified CSS -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div class="container">
<h1 class="center">
Welcome to the Chat Room
</h1>
<div class="row">
<div class="row">
<div class="input-field col l12 m12 s12">
<input value="" id="first_name2" type="text" class="validate">
<label class="active" for="first_name2">Name</label>
</div>
</div>
<div class="row">
<div class="input-field col l12 m12 s12">
<input value="" id="message" type="text" class="validate">
<label class="active" for="message">Message</label>
</div>
</div>
<div class="row">
<a class="waves-effect waves-light btn" id='send'><i class="material-icons left">send</i>Send</a>
</div>
</div>
<div class="row">
<div id="messages">
</div>
</div>
</div>
<script>
var socket = io();
$(document).ready(function() {
// Document load
// Materialize requirements
M.updateTextFields();
// when send button is clicked, add the default json object to the messages div
$('#send').click(()=>{
postMessage({name:$('#first_name2').val(), text:$('#message').val()});
})
// Load messages from backend api on document load
getMessages()
});
socket.on('new_message', addMessages);
function addMessages(message){
// Add message on click
$('#messages').append(`<div class="row"><b>${message.name}</b><p>${message.text}</p></div>`)
}
function postMessage(message)
{
console.log(message);
$.post('http://127.0.0.1:3000/messages', message);
}
function getMessages()
{
// Load messages from backend api endpoint
$.get('http://127.0.0.1:3000/messages', (data)=>{
// For each message object run addMessages function
data.forEach(addMessages);
})
}
</script>
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>