-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
148 lines (130 loc) · 5.28 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Socket.IO chat</title>
<style>
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
#footer { position: fixed; bottom: 0; left: 0; right: 0; display: flex; flex-direction: column;}
#form { background: rgba(0, 0, 0, 0.15); padding: 0.25rem; display: flex; height: 3rem; box-sizing: border-box; backdrop-filter: blur(10px); }
#input { border: none; padding: 0 1rem; flex-grow: 1; border-radius: 2rem; margin: 0.25rem; }
#input:focus { outline: none; }
#form > button { background: #333; border: none; padding: 0 1rem; margin: 0.25rem; border-radius: 3px; outline: none; color: #fff; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages > li { padding: 0.5rem 1rem; }
#messages > li:nth-child(odd) { background: #efefef; }
</style>
</head>
<body>
<ul id="messages" style="width: 80%;"></ul>
<div style="width: 20%; position: fixed; right: 0; top: 0; bottom: 0;">
<p id="name" style="float: right; border: black 1px solid; padding: 10px;"></p>
<ul id="users"></ul>
</div>
<div id="footer">
<span id="writingMessage" style="padding-left: 5px; padding-bottom: 3px;"></span>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button>
</form>
</div>
<script src="/socket.io/socket.io.js"></script>
<script>
let messages = document.getElementById('messages');
let form = document.getElementById('form');
let input = document.getElementById('input');
let userList = document.getElementById('users');
let writingMessage = document.getElementById('writingMessage');
let users = [];
let usersWriting = [];
let roomId = window.location.pathname
while(roomId == '/' || roomId == '/null' || roomId == null || roomId == ''){
roomId = prompt('Entrez un nom de salon');
}
if(window.location.pathname != roomId)
window.location.href = `${roomId}`
let pseudo = null;
while(isEmptyOrSpaces(pseudo) || pseudo.length > 20){
pseudo = prompt("Choisissez un pseudo");
pseudo = encodeURI(pseudo).replaceAll('%20', ' ');
}
document.title = `${roomId.split('/')[1]} - ${pseudo}`;
let socket = io();
socket.on("connect", () => {
socket.emit('createConnection',pseudo,new URL(window.location.href).pathname.split('/')[1]);
console.log('Connexion au serveur');
});
form.addEventListener('submit', function(e) {
e.preventDefault();
if (input.value && input.value != '' && input.value.length < 500) {
socket.emit('chat message', input.value);
input.value = '';
usersWriting.splice(usersWriting.indexOf(pseudo),1);
refreshWritingMessage();
}
});
input.onfocus = function(){
socket.emit('writing');
}
input.onblur = function(){
socket.emit('not writing');
}
/*socket.on('is writing', (pseudo) => {
usersWriting.push(pseudo);
refreshWritingMessage();
})*/
socket.on('is not writing', (pseudo) => {
usersWriting.splice(usersWriting.indexOf(pseudo),1);
refreshWritingMessage();
})
socket.on('chat message', function(pseudo, msg, time) {
let item = document.createElement('li');
item.textContent = `${time} ${pseudo}: ${msg}`;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
usersWriting.splice(usersWriting.indexOf(pseudo),1);
refreshWritingMessage();
});
//premier échange avec le nouveau socket
socket.on('me connected', function(pseudo, roomUsers){
users = roomUsers;
document.getElementById('name').innerHTML = pseudo;
})
//un nouvel utilisateur vient de se connecter
socket.on('user connected', function(pseudo){
users.push(pseudo);
let item = document.createElement('li');
let i = document.createElement('i');
i.textContent = `${pseudo} vient d'arrivé`;
item.appendChild(i)
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
userListUpd(users);
console.log(pseudo + ' vient d\'arriver');
})
socket.on('user deconnected', function(pseudo){
let i = users.indexOf(pseudo);
users.splice(i,1);
console.log(pseudo + ' est partit');
userListUpd(users);
})
function userListUpd(roomUsers){
userList.innerHTML = '';
roomUsers.forEach(user => {
let item = document.createElement('li');
item.innerHTML = user;
userList.appendChild(item);
})
}
function refreshWritingMessage(){
if(usersWriting.length == 1)
writingMessage.innerHTML = `${usersWriting} est en train d'écrire`;
else if(usersWriting > 1)
writingMessage.innerHTML = `${usersWriting} sont en train d'écrire`;
else writingMessage.innerHTML = '';
}
function isEmptyOrSpaces(str){
return str === null || str.match(/^ *$/) !== null;
}
</script>
</body>
</html>