Initial commit.
This commit is contained in:
12
public/index.html
Normal file
12
public/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Gub</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="messages"></div>
|
||||
<input id="input" autocomplete="off" />
|
||||
<button onclick="sendMessage()">Send</button>
|
||||
<script src="main.js"></script>
|
||||
</body>
|
||||
</html>
|
22
public/main.js
Normal file
22
public/main.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const ws = new WebSocket(`ws://${location.host}`);
|
||||
const messagesDiv = document.getElementById("messages");
|
||||
const input = document.getElementById("input");
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const msg = document.createElement("div");
|
||||
msg.textContent = event.data;
|
||||
messagesDiv.appendChild(msg);
|
||||
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
||||
};
|
||||
|
||||
function sendMessage() {
|
||||
const text = input.value;
|
||||
if (text.trim()) {
|
||||
ws.send(text);
|
||||
input.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
input.addEventListener("keypress", (e) => {
|
||||
if (e.key === "Enter") sendMessage();
|
||||
});
|
Reference in New Issue
Block a user