-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
83 lines (75 loc) · 2.5 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Chat</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<style>
.chat {
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.chat-p {
background: skyblue;
width: 60%;
border-radius: 2px;
max-height: 60%;
height: 40%;
overflow: scroll;
border: 1px solid #181818;
}
.messages p {
padding: 2px;
}
.chat-other {
padding: 6px;
}
</style>
<div id="nicknameForm">
<input id="nicknameInput" placeholder="Enter your nickname" />
<button onclick="joinChat()">Join Chat</button>
</div>
<div class="chat">
<div class="chat-p">
<ul id="messages"></ul>
</div>
<div class="chat-other">
<input id="messageInput" placeholder="Напишите сообщение..." autocomplete="off" disabled />
<button id="sendButton" onclick="sendMessage()" disabled>Send</button>
</div>
</div>
<script src="https://cdn.socket.io/4.3.2/socket.io.js"></script>
<script>
const socket = io();
let nickname = '';
function joinChat() {
nickname = document.getElementById('nicknameInput').value;
if (nickname.trim() !== '') {
socket.emit('join', nickname);
document.getElementById('nicknameForm').style.display = 'none';
document.getElementById('messageInput').disabled = false;
document.getElementById('sendButton').disabled = false;
}
}
function sendMessage() {
const messageInput = document.getElementById('messageInput');
const message = messageInput.value;
if (message.trim() !== '') {
socket.emit('chat message', message);
messageInput.value = '';
}
}
socket.on('chat message', (msg) => {
const item = document.createElement('p');
item.textContent = msg;
document.getElementById('messages').appendChild(item);
});
</script>
</body>
</html>