-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
99 lines (89 loc) · 3.16 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Anonymous Chat</title>
<link href="/style.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/emoji-picker-element/1.0.1/emoji-picker-element.min.css">
<style>
/* Additional styling */
#user-count {
font-size: 1.2rem;
font-weight: bold;
}
#messages {
border: 1px solid #444;
border-radius: 8px;
padding: 10px;
background: #333;
}
#send {
width: 100px; /* Adjusted button width */
}
</style>
</head>
<body class="bg-gray-800 text-white flex items-center justify-center min-h-screen">
<div id="chat-container" class="w-full max-w-md bg-gray-900 p-4 rounded-lg shadow-lg">
<div id="user-count" class="text-center mb-4 text-lg">Users Online: 0</div>
<div id="chat" class="flex flex-col h-80">
<div id="messages" class="flex-1 overflow-y-auto mb-4"></div>
<div class="flex items-center">
<input id="message" type="text" class="w-full p-2 bg-gray-700 rounded" placeholder="Type a message...">
<button id="send" class="w-24 p-2 ml-2 bg-blue-500 rounded transition-transform transform hover:scale-105">Send</button>
</div>
</div>
</div>
<!-- Background Music -->
<audio id="bg-music" src="background-music.mp3" loop></audio>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
const messages = document.getElementById('messages');
const messageInput = document.getElementById('message');
const sendButton = document.getElementById('send');
const userCountDisplay = document.getElementById('user-count');
const bgMusic = document.getElementById('bg-music');
// Play background music
bgMusic.volume = 0.2; // Set volume level
bgMusic.play();
function addMessage(message) {
const messageElement = document.createElement('div');
messageElement.classList.add('p-2', 'bg-gray-800', 'rounded', 'mb-2');
const gifUrlPattern = /(https?:\/\/.*\.(?:png|jpg|jpeg|gif))/i;
if (gifUrlPattern.test(message)) {
const img = document.createElement('img');
img.src = message;
img.classList.add('w-full', 'rounded');
messageElement.appendChild(img);
} else {
messageElement.textContent = message;
}
messages.appendChild(messageElement);
messages.scrollTop = messages.scrollHeight;
}
socket.on('chat message', (msg) => {
addMessage(msg);
});
socket.on('user count', (count) => {
userCountDisplay.textContent = `Users Online: ${count}`;
});
sendButton.addEventListener('click', () => {
const msg = messageInput.value;
if (msg.trim()) {
sendButton.classList.add('sending');
socket.emit('chat message', msg);
messageInput.value = '';
setTimeout(() => {
sendButton.classList.remove('sending');
}, 600);
}
});
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendButton.click();
}
});
</script>
</body>
</html>