-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.html
49 lines (43 loc) · 1.1 KB
/
page.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Пример socket.io</title>
<style>
#form {
position: fixed;
bottom: 0;
left: 0;
right: 0;
display: flex;
}
</style>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div id="chat">
</div>
<form id="form">
<input id="message" type="text"/>
<button>send</button>
</form>
<script>
let socket = io();
const form = document.getElementById('form');
const messageBox = document.getElementById('message');
const messages = document.getElementById('chat');
form.addEventListener('submit', (e) => {
e.preventDefault();
if (messageBox.value) {
socket.emit('newMessage', messageBox.value);
messageBox.value = '';
}
});
socket.on('incomingMessage', function (msg) {
const item = document.createElement('div');
item.textContent = msg;
messages.appendChild(item);
});
</script>
</body>
</html>