|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Simple Chat Frontend</title> |
| 5 | + <style> |
| 6 | + #messages { |
| 7 | + border: 1px solid black; |
| 8 | + height: 300px; |
| 9 | + overflow-y: auto; |
| 10 | + margin-bottom: 10px; |
| 11 | + } |
| 12 | + </style> |
| 13 | +</head> |
| 14 | +<body> |
| 15 | +<h2>Register</h2> |
| 16 | +<input id="regUsername" placeholder="Username" /> |
| 17 | +<input id="regPassword" type="password" placeholder="Password" /> |
| 18 | +<button id="registerBtn">Register</button> |
| 19 | +<p id="registerStatus"></p> |
| 20 | + |
| 21 | +<h2>Login</h2> |
| 22 | +<input id="loginUsername" placeholder="Username" /> |
| 23 | +<input id="loginPassword" type="password" placeholder="Password" /> |
| 24 | +<button id="loginBtn">Login</button> |
| 25 | +<p id="loginStatus"></p> |
| 26 | + |
| 27 | +<div id="chat" style="display:none;"> |
| 28 | + <h2>Chat</h2> |
| 29 | + <div id="messages"></div> |
| 30 | + <input id="messageInput" placeholder="Type a message" /> |
| 31 | + <button id="sendBtn">Send</button> |
| 32 | + <button id="logoutBtn">Logout</button> |
| 33 | +</div> |
| 34 | + |
| 35 | +<script> |
| 36 | + let token = null; |
| 37 | + let ws = null; |
| 38 | + |
| 39 | + document.getElementById('registerBtn').onclick = async () => { |
| 40 | + const username = document.getElementById('regUsername').value.trim(); |
| 41 | + const password = document.getElementById('regPassword').value.trim(); |
| 42 | + |
| 43 | + if (!username || !password) { |
| 44 | + document.getElementById('registerStatus').innerText = 'Fill in username and password'; |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const res = await fetch('http://localhost:8080/register', { |
| 49 | + method: 'POST', |
| 50 | + headers: {'Content-Type': 'application/json'}, |
| 51 | + body: JSON.stringify({username, password}) |
| 52 | + }); |
| 53 | + |
| 54 | + if (res.ok) { |
| 55 | + document.getElementById('registerStatus').innerText = 'Registered successfully. Please login.'; |
| 56 | + } else { |
| 57 | + const data = await res.json(); |
| 58 | + document.getElementById('registerStatus').innerText = 'Registration failed: ' + (data.error || 'Unknown error'); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + document.getElementById('loginBtn').onclick = async () => { |
| 63 | + const username = document.getElementById('loginUsername').value.trim(); |
| 64 | + const password = document.getElementById('loginPassword').value.trim(); |
| 65 | + |
| 66 | + if (!username || !password) { |
| 67 | + document.getElementById('loginStatus').innerText = 'Fill in username and password'; |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const res = await fetch('http://localhost:8080/login', { |
| 72 | + method: 'POST', |
| 73 | + headers: {'Content-Type': 'application/json'}, |
| 74 | + body: JSON.stringify({username, password}) |
| 75 | + }); |
| 76 | + |
| 77 | + if (res.ok) { |
| 78 | + const data = await res.json(); |
| 79 | + token = data.token; |
| 80 | + document.getElementById('loginStatus').innerText = ''; |
| 81 | + document.getElementById('chat').style.display = 'block'; |
| 82 | + await loadMessages(); |
| 83 | + setupWebSocket(); |
| 84 | + } else { |
| 85 | + document.getElementById('loginStatus').innerText = 'Login failed'; |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + async function loadMessages() { |
| 90 | + const res = await fetch('http://localhost:8080/messages', { |
| 91 | + headers: { |
| 92 | + 'Authorization': 'Bearer ' + token |
| 93 | + } |
| 94 | + }); |
| 95 | + if (!res.ok) { |
| 96 | + console.error('Failed to load messages'); |
| 97 | + return; |
| 98 | + } |
| 99 | + const messages = await res.json(); |
| 100 | + console.log(messages); |
| 101 | + const container = document.getElementById('messages'); |
| 102 | + container.innerHTML = ''; |
| 103 | + |
| 104 | + messages.forEach(msgObj => { |
| 105 | + const msgDiv = document.createElement('div'); |
| 106 | + msgDiv.style.marginBottom = "15px"; |
| 107 | + |
| 108 | + const sender = document.createElement('div'); |
| 109 | + sender.textContent = msgObj.UserID || `User ${msgObj.user_id}`; |
| 110 | + sender.style.fontWeight = 'bold'; |
| 111 | + |
| 112 | + const content = document.createElement('div'); |
| 113 | + content.textContent = msgObj.Content; |
| 114 | + |
| 115 | + const timestamp = document.createElement('div'); |
| 116 | + const dt = new Date(msgObj.CreatedAt || msgObj.timestamp); |
| 117 | + timestamp.textContent = dt.toLocaleDateString() + ' ' + dt.toLocaleTimeString(); |
| 118 | + timestamp.style.fontSize = 'small'; |
| 119 | + timestamp.style.color = 'gray'; |
| 120 | + |
| 121 | + msgDiv.appendChild(sender); |
| 122 | + msgDiv.appendChild(content); |
| 123 | + msgDiv.appendChild(timestamp); |
| 124 | + |
| 125 | + container.appendChild(msgDiv); |
| 126 | + }); |
| 127 | + |
| 128 | + container.scrollTop = container.scrollHeight; |
| 129 | + } |
| 130 | + |
| 131 | + function setupWebSocket() { |
| 132 | + ws = new WebSocket(`ws://localhost:8080/ws?token=${token}`); |
| 133 | + |
| 134 | + ws.onmessage = (event) => { |
| 135 | + const msgObj = JSON.parse(event.data); |
| 136 | + |
| 137 | + const msgDiv = document.createElement('div'); |
| 138 | + msgDiv.style.marginBottom = "15px"; |
| 139 | + |
| 140 | + const sender = document.createElement('div'); |
| 141 | + sender.textContent = msgObj.user; |
| 142 | + sender.style.fontWeight = 'bold'; |
| 143 | + |
| 144 | + const content = document.createElement('div'); |
| 145 | + content.textContent = msgObj.content; |
| 146 | + |
| 147 | + const timestamp = document.createElement('div'); |
| 148 | + const dt = new Date(msgObj.timestamp); |
| 149 | + timestamp.textContent = dt.toLocaleDateString(); |
| 150 | + timestamp.style.fontSize = 'small'; |
| 151 | + timestamp.style.color = 'gray'; |
| 152 | + |
| 153 | + msgDiv.appendChild(sender); |
| 154 | + msgDiv.appendChild(content); |
| 155 | + msgDiv.appendChild(timestamp); |
| 156 | + |
| 157 | + const messagesContainer = document.getElementById('messages'); |
| 158 | + messagesContainer.appendChild(msgDiv); |
| 159 | + messagesContainer.scrollTop = messagesContainer.scrollHeight; |
| 160 | + }; |
| 161 | + |
| 162 | + ws.onclose = () => { |
| 163 | + alert('WebSocket closed.'); |
| 164 | + logout(); |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + document.getElementById('sendBtn').onclick = async () => { |
| 169 | + const content = document.getElementById('messageInput').value.trim(); |
| 170 | + if (!content) return; |
| 171 | + |
| 172 | + await fetch('http://localhost:8080/messages', { |
| 173 | + method: 'POST', |
| 174 | + headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, |
| 175 | + body: JSON.stringify({content}) |
| 176 | + }); |
| 177 | + |
| 178 | + const msgDiv = document.createElement('div'); |
| 179 | + msgDiv.textContent = 'You: ' + content; |
| 180 | + document.getElementById('messages').appendChild(msgDiv); |
| 181 | + document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight; |
| 182 | + document.getElementById('messageInput').value = ''; |
| 183 | + }; |
| 184 | + |
| 185 | + document.getElementById('logoutBtn').onclick = () => { |
| 186 | + logout(); |
| 187 | + }; |
| 188 | + |
| 189 | + function logout() { |
| 190 | + token = null; |
| 191 | + if (ws) ws.close(); |
| 192 | + document.getElementById('chat').style.display = 'none'; |
| 193 | + document.getElementById('loginStatus').innerText = ''; |
| 194 | + document.getElementById('messages').innerHTML = ''; |
| 195 | + } |
| 196 | +</script> |
| 197 | +</body> |
| 198 | +</html> |
0 commit comments