Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const messageContainer = document.getElementById('message-container');
+ const messageForm = document.getElementById('chat-form');
+ const messageInput = document.getElementById('message-input');
+ const onlineUsers = document.getElementById('online-users');
+ const chatRooms = document.getElementById('chat-rooms');
+
+ let ws;
+
+ function connectWebSocket() {
+ ws = new WebSocket(`wss://${window.location.host}/ws`);
+
+ ws.onopen = () => {
+ console.log('Connected to WebSocket server');
+ addSystemMessage('Connected to chat server');
+ };
+
+ ws.onmessage = (event) => {
+ const message = JSON.parse(event.data);
+ displayMessage(message);
+ };
+
+ ws.onclose = () => {
+ console.log('Disconnected from WebSocket server');
+ addSystemMessage('Disconnected from chat server. Reconnecting...');
+ setTimeout(connectWebSocket, 3000);
+ };
+
+ ws.onerror = (error) => {
+ console.error('WebSocket error:', error);
+ addSystemMessage('Error connecting to chat server');
+ };
+ }
+
+ function displayMessage(message) {
+ const messageElement = document.createElement('div');
+ messageElement.classList.add('message');
+
+ const timestamp = new Date().toLocaleTimeString();
+ messageElement.innerHTML = `
+ ${timestamp}
+ ${message.username}
+ ${escapeHtml(message.content)}
+ `;
+
+ messageContainer.appendChild(messageElement);
+ messageContainer.scrollTop = messageContainer.scrollHeight;
+ }
+
+ function addSystemMessage(text) {
+ const messageElement = document.createElement('div');
+ messageElement.classList.add('system-message');
+ messageElement.textContent = text;
+ messageContainer.appendChild(messageElement);
+ messageContainer.scrollTop = messageContainer.scrollHeight;
+ }
+
+ function escapeHtml(unsafe) {
+ return unsafe
+ .replace(/&/g, "&")
+ .replace(/
+ .replace(/>/g, ">")
+ .replace(/"/g, """)
+ .replace(/'/g, "'");
+ }
+
+ messageForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+
+ const message = messageInput.value.trim();
+ if (message && ws.readyState === WebSocket.OPEN) {
+ ws.send(JSON.stringify({
+ type: 'message',
+ content: message
+ }));
+ messageInput.value = '';
+ }
+ });
+
+ // Initialize demo chat rooms
+ const demoRooms = ['General', 'Tech', 'Random'];
+ demoRooms.forEach(room => {
+ const li = document.createElement('li');
+ li.textContent = room;
+ chatRooms.appendChild(li);
+ });
+
+ // Initialize demo online users
+ const demoUsers = ['Alice', 'Bob', 'Charlie'];
+ demoUsers.forEach(user => {
+ const li = document.createElement('li');
+ li.textContent = user;
+ onlineUsers.appendChild(li);
+ });
+
+ // Start WebSocket connection
+ connectWebSocket();
+ });
Changed around line 1
+ :root {
+ --primary-color: #2d3436;
+ --secondary-color: #00b894;
+ --bg-color: #f5f6fa;
+ --text-color: #2d3436;
+ --border-radius: 12px;
+ --transition: all 0.3s ease;
+ }
+
+ * {
+ margin: 0;
+ padding: 0;
+ box-sizing: border-box;
+ }
+
+ body {
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
+ line-height: 1.6;
+ color: var(--text-color);
+ background: var(--bg-color);
+ }
+
+ .header-main {
+ background: var(--primary-color);
+ color: white;
+ padding: 1rem 2rem;
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
+ }
+
+ .nav-links a {
+ color: white;
+ text-decoration: none;
+ margin-left: 2rem;
+ transition: var(--transition);
+ }
+
+ .nav-links a:hover {
+ color: var(--secondary-color);
+ }
+
+ .chat-container {
+ display: grid;
+ grid-template-columns: 250px 1fr 250px;
+ height: calc(100vh - 140px);
+ gap: 1rem;
+ padding: 1rem;
+ }
+
+ .user-list, .room-list {
+ background: white;
+ border-radius: var(--border-radius);
+ padding: 1rem;
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
+ }
+
+ .chat-main {
+ display: flex;
+ flex-direction: column;
+ background: white;
+ border-radius: var(--border-radius);
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
+ }
+
+ .messages {
+ flex-grow: 1;
+ padding: 1rem;
+ overflow-y: auto;
+ }
+
+ .message-form {
+ display: flex;
+ padding: 1rem;
+ gap: 1rem;
+ border-top: 1px solid #eee;
+ }
+
+ #message-input {
+ flex-grow: 1;
+ padding: 0.8rem;
+ border: 2px solid #eee;
+ border-radius: var(--border-radius);
+ transition: var(--transition);
+ }
+
+ #message-input:focus {
+ outline: none;
+ border-color: var(--secondary-color);
+ }
+
+ .send-button {
+ background: var(--secondary-color);
+ color: white;
+ border: none;
+ padding: 0.8rem 1.5rem;
+ border-radius: var(--border-radius);
+ cursor: pointer;
+ transition: var(--transition);
+ }
+
+ .send-button:hover {
+ transform: translateY(-2px);
+ box-shadow: 0 2px 8px rgba(0,184,148,0.4);
+ }
+
+ .footer-main {
+ background: var(--primary-color);
+ color: white;
+ padding: 1rem 2rem;
+ text-align: center;
+ }
+
+ .footer-main nav {
+ margin-top: 0.5rem;
+ }
+
+ .footer-main a {
+ color: white;
+ text-decoration: none;
+ margin: 0 1rem;
+ transition: var(--transition);
+ }
+
+ .footer-main a:hover {
+ color: var(--secondary-color);
+ }
+
+ @media (max-width: 768px) {
+ .chat-container {
+ grid-template-columns: 1fr;
+ }
+
+ .user-list, .room-list {
+ display: none;
+ }
+ }