-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (48 loc) · 1.68 KB
/
script.js
File metadata and controls
58 lines (48 loc) · 1.68 KB
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
const socket = io({
autoConnect: false // Do not connect automatically
});
// DOM Elements
const passwordContainer = document.getElementById('password-container');
const passwordInput = document.getElementById('password-input');
const passwordSubmit = document.getElementById('password-submit');
const errorMessage = document.getElementById('error-message');
const chatContainer = document.getElementById('chat-container');
const messages = document.getElementById('messages');
const form = document.getElementById('form');
const input = document.getElementById('input');
const bgColorPicker = document.getElementById('bg-color');
// --- Password Handling ---
passwordSubmit.addEventListener('click', () => {
const password = passwordInput.value;
socket.auth = { password };
socket.connect();
});
socket.on("connect_error", (err) => {
if (err.message === "invalid password") {
errorMessage.classList.remove('hidden');
passwordInput.value = '';
}
});
socket.on('connect', () => {
passwordContainer.classList.add('hidden');
chatContainer.classList.remove('hidden');
errorMessage.classList.add('hidden');
});
// --- Chat Functionality ---
form.addEventListener('submit', (e) => {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});
socket.on('chat message', (msg) => {
const item = document.createElement('li');
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
// --- Background Change Functionality ---
bgColorPicker.addEventListener('input', (e) => {
chatContainer.style.backgroundColor = e.target.value;
});