create initial frontend.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Shagram Chat</title>
|
||||
<style>
|
||||
body { font-family: Arial; margin: 20px; }
|
||||
#messages {
|
||||
height: 300px;
|
||||
border: 1px solid #ccc;
|
||||
overflow-y: auto;
|
||||
padding: 10px;
|
||||
margin-bottom: 10px;
|
||||
background: #f9f9f9;
|
||||
}
|
||||
.message {
|
||||
margin: 5px 0;
|
||||
padding: 8px;
|
||||
background: #e3f2fd;
|
||||
border-radius: 5px;
|
||||
border-left: 3px solid #2196F3;
|
||||
}
|
||||
input { padding: 8px; width: 300px; }
|
||||
button { padding: 8px 15px; cursor: pointer; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>📱 Shagram Chat</h1>
|
||||
|
||||
<div>
|
||||
<input type="text" id="roomInput" placeholder="Room name" value="general">
|
||||
<button onclick="connectRoom()">Connect</button>
|
||||
</div>
|
||||
|
||||
<div id="messages"></div>
|
||||
|
||||
<div>
|
||||
<input type="text" id="messageInput" placeholder="Type message..."
|
||||
onkeypress="if(event.key==='Enter') sendMessage()">
|
||||
<button onclick="sendMessage()">Send</button>
|
||||
</div>
|
||||
|
||||
<script src="js/chat.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,43 @@
|
||||
let ws = null;
|
||||
const messagesDiv = document.getElementById('messages');
|
||||
const messageInput = document.getElementById('messageInput');
|
||||
const roomInput = document.getElementById('roomInput');
|
||||
|
||||
function connectRoom() {
|
||||
const room = roomInput.value || 'general';
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
ws = new WebSocket(`${protocol}//localhost:8080/ws/${room}`);
|
||||
|
||||
ws.onopen = function() {
|
||||
console.log('Connected to room: ' + room);
|
||||
messagesDiv.innerHTML = '<div class="message">✅ Connected to ' + room + '</div>';
|
||||
};
|
||||
|
||||
ws.onmessage = function(event) {
|
||||
const msgDiv = document.createElement('div');
|
||||
msgDiv.className = 'message';
|
||||
msgDiv.textContent = event.data;
|
||||
messagesDiv.appendChild(msgDiv);
|
||||
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
||||
};
|
||||
|
||||
ws.onerror = function(error) {
|
||||
console.error('WebSocket error:', error);
|
||||
messagesDiv.innerHTML += '<div class="message">❌ Error: ' + error + '</div>';
|
||||
};
|
||||
|
||||
ws.onclose = function() {
|
||||
messagesDiv.innerHTML += '<div class="meassage">⛔ Disconnected</div>';
|
||||
};
|
||||
}
|
||||
|
||||
function sendMessage() {
|
||||
const text = messageInput.value.trim();
|
||||
if (!text || !ws || ws.readyState !== WebSocket.OPEN) {
|
||||
alert('Not connected or empty message');
|
||||
return;
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify({text: text}));
|
||||
messageInput.value = '';
|
||||
}
|
||||
Reference in New Issue
Block a user