-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
123 lines (112 loc) · 3.33 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!doctype html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Chat de Nounou</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="css/custom.css">
</head>
<body>
<style>
.text-center{
color: beige;
text-align: center;
}
#messages{
border: 1px solid black;
height: 70vh;
background-color: beige;
}
#messages p{
margin:0;
padding: 10px;
}
#messages :nth-child(odd){
background-color: #90EE90;
}
form{
display: flex;
width: 70vw;
margin: auto;
padding: 20px;
}
#name{
flex:1;
padding: 15px;
}
#message{
flex:5;
}
#button{
flex:1;
padding:15px;
border:none;
background-color: green;
color:white;
}
body{
background-color: black;
}
</style>
<!-- PARTI CLIENT -->
<h1 class="text-center">Chat de Nounou</h1>
<div class="row">
<div class="col-3"></div>
<div class="col-6">
<div id="messages">
</div>
</div>
</div>
<form id="form">
<input type="text" id="name" placeholder="Entrez votre nom">
<input type="text" id="message" placeholder="Entrez votre message">
<button id="button">Envoyer</button>
</form>
<!-- On cherche socket.io.js -->
<script src="/socket.io/socket.io.js"></script>
<script>
// On se connect au serveur socket
const socket = io();
// window.onload = on verifie que nos ressources sois bien chargé
window.onload = () => {
// On écoute l'évènement submit après la validation du formulaire
// On va ajouter un evenement sur le selecteur du formulaire quand il est soumis
document.querySelector("form").addEventListener("submit",(e) => {
e.preventDefault();
// PREVENT DEFAULT va empecher l'envoi du formulaire
const name = document.querySelector("#name")
const message = document.querySelector("#message");
// On envoie le message
socket.emit("chat_message", { name: name.value, message: message.value })
});
// On écoute l'evenement "chat_message"
socket.on("chat_message", (msg) => {
console.dir(msg);
document.querySelector("#messages").innerHTML += '<p>' + '<b>' + msg.name + '</b>' + ' dit ' + '<br>' + msg.message + '</p>' ;
// Quand on recois un message il s'affiche :)
})
}
</script>
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
m = checkTime(m);
document.getElementById('time').innerHTML =
"Local Time : "+h + ":" + m;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
startTime();
</script>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>