Create public/index.html
Browse files- public/index.html +43 -0
public/index.html
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>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) {
|
30 |
+
socket.emit('chat message', input.value);
|
31 |
+
input.value = '';
|
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 |
+
});
|
41 |
+
</script>
|
42 |
+
</body>
|
43 |
+
</html>
|