Spaces:
Sleeping
Sleeping
Create public/index.html
Browse files- public/index.html +80 -0
public/index.html
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>CustomBoxServer</title>
|
7 |
+
<style>
|
8 |
+
body { font-family: Arial, sans-serif; }
|
9 |
+
#messages { list-style-type: none; padding: 0; }
|
10 |
+
#messages li { margin: 5px 0; }
|
11 |
+
#chat { display: flex; }
|
12 |
+
#chat input { flex: 1; }
|
13 |
+
#chat button { margin-left: 5px; }
|
14 |
+
</style>
|
15 |
+
</head>
|
16 |
+
<body>
|
17 |
+
|
18 |
+
<h1>Chat Application</h1>
|
19 |
+
<ul id="messages"></ul>
|
20 |
+
|
21 |
+
<div id="chat">
|
22 |
+
<input id="username" type="text" placeholder="Your name" />
|
23 |
+
<input id="color" type="text" placeholder="Your color (e.g., red)" />
|
24 |
+
<input id="message" type="text" placeholder="Type a message..." />
|
25 |
+
<button id="send">Send</button>
|
26 |
+
</div>
|
27 |
+
|
28 |
+
<script src="/socket.io/socket.io.js"></script>
|
29 |
+
<script>
|
30 |
+
const socket = io("http://localhost:8086", {
|
31 |
+
path: '/socket.io/',
|
32 |
+
transports: ['websocket', 'polling'], // Ensure compatibility with older clients
|
33 |
+
});
|
34 |
+
|
35 |
+
let username;
|
36 |
+
|
37 |
+
document.getElementById('send').onclick = function() {
|
38 |
+
if (!username) {
|
39 |
+
username = document.getElementById('username').value;
|
40 |
+
const color = document.getElementById('color').value;
|
41 |
+
if (username && color) {
|
42 |
+
socket.emit("user joined", username, color); // Emit user joined event
|
43 |
+
}
|
44 |
+
}
|
45 |
+
|
46 |
+
const messageInput = document.getElementById('message');
|
47 |
+
|
48 |
+
if (messageInput.value) {
|
49 |
+
// Send message using socket.send()
|
50 |
+
socket.send({ msg: messageInput.value });
|
51 |
+
messageInput.value = ''; // Clear input field after sending
|
52 |
+
}
|
53 |
+
};
|
54 |
+
|
55 |
+
// Listen for messages from server using socket.on()
|
56 |
+
socket.on("message", function(data) {
|
57 |
+
const item = document.createElement("li");
|
58 |
+
item.style.color = data.color || "black";
|
59 |
+
item.textContent = `${data.nick}: ${data.msg}`;
|
60 |
+
document.getElementById("messages").appendChild(item);
|
61 |
+
});
|
62 |
+
|
63 |
+
// Listen for user join notifications
|
64 |
+
socket.on("user joined", function(data) {
|
65 |
+
const item = document.createElement("li");
|
66 |
+
item.style.color = data.color || "black";
|
67 |
+
item.textContent = `${data.username} has joined the chat`;
|
68 |
+
document.getElementById("messages").appendChild(item);
|
69 |
+
});
|
70 |
+
|
71 |
+
// Listen for user left notifications
|
72 |
+
socket.on("user left", function(data) {
|
73 |
+
const item = document.createElement("li");
|
74 |
+
item.textContent = `${data.username} has left the chat`;
|
75 |
+
document.getElementById("messages").appendChild(item);
|
76 |
+
});
|
77 |
+
</script>
|
78 |
+
|
79 |
+
</body>
|
80 |
+
</html>
|