1tbfree commited on
Commit
9d1d950
·
verified ·
1 Parent(s): c388863

Update public/index.html

Browse files
Files changed (1) hide show
  1. public/index.html +35 -8
public/index.html CHANGED
@@ -3,27 +3,53 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Socket.IO Chat</title>
7
  <style>
8
  ul { list-style-type: none; padding: 0; }
9
  li { padding: 8px; margin-bottom: 10px; background: #f1f1f1; }
 
10
  </style>
11
  </head>
12
  <body>
13
- <h1>Socket.IO Chat</h1>
 
 
 
 
 
 
14
  <ul id="messages"></ul>
15
- <form id="form" action="">
16
- <input id="input" autocomplete="off" /><button>Send</button>
 
17
  </form>
18
 
19
  <script src="/socket.io/socket.io.js"></script>
20
  <script>
21
  const socket = io();
22
-
 
23
  const form = document.getElementById('form');
24
  const input = document.getElementById('input');
25
- const messages = document.getElementById('messages');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
 
27
  form.addEventListener('submit', function(e) {
28
  e.preventDefault();
29
  if (input.value) {
@@ -32,9 +58,10 @@
32
  }
33
  });
34
 
35
- socket.on('chat message', function(msg) {
 
36
  const item = document.createElement('li');
37
- item.textContent = msg;
38
  messages.appendChild(item);
39
  window.scrollTo(0, document.body.scrollHeight);
40
  });
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>HugVPS Chat</title>
7
  <style>
8
  ul { list-style-type: none; padding: 0; }
9
  li { padding: 8px; margin-bottom: 10px; background: #f1f1f1; }
10
+ #username-form { display: none; }
11
  </style>
12
  </head>
13
  <body>
14
+ <h1>HugVPS Chat</h1>
15
+
16
+ <div id="username-form">
17
+ <input id="username" placeholder="Enter your username" />
18
+ <button id="set-username">Set Username</button>
19
+ </div>
20
+
21
  <ul id="messages"></ul>
22
+ <form id="form" action="" style="display: none;">
23
+ <input id="input" autocomplete="off" placeholder="Type your message..." />
24
+ <button>Send</button>
25
  </form>
26
 
27
  <script src="/socket.io/socket.io.js"></script>
28
  <script>
29
  const socket = io();
30
+ const usernameForm = document.getElementById('username-form');
31
+ const messages = document.getElementById('messages');
32
  const form = document.getElementById('form');
33
  const input = document.getElementById('input');
34
+ const usernameInput = document.getElementById('username');
35
+ const setUsernameButton = document.getElementById('set-username');
36
+
37
+ // Show username form on page load
38
+ window.onload = () => {
39
+ usernameForm.style.display = 'block';
40
+ };
41
+
42
+ // Set username
43
+ setUsernameButton.addEventListener('click', () => {
44
+ const username = usernameInput.value.trim();
45
+ if (username) {
46
+ socket.emit('set username', username);
47
+ usernameForm.style.display = 'none';
48
+ form.style.display = 'block';
49
+ }
50
+ });
51
 
52
+ // Handle message submission
53
  form.addEventListener('submit', function(e) {
54
  e.preventDefault();
55
  if (input.value) {
 
58
  }
59
  });
60
 
61
+ // Listen for chat messages
62
+ socket.on('chat message', function(data) {
63
  const item = document.createElement('li');
64
+ item.textContent = `${data.username}: ${data.message}`;
65
  messages.appendChild(item);
66
  window.scrollTo(0, document.body.scrollHeight);
67
  });