DmitrMakeev commited on
Commit
dd284ea
·
verified ·
1 Parent(s): b57cee1

Create chat.html

Browse files
Files changed (1) hide show
  1. chat.html +306 -0
chat.html ADDED
@@ -0,0 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="ru">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Simple Chat</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
+ height: 100vh;
18
+ display: flex;
19
+ justify-content: center;
20
+ align-items: center;
21
+ }
22
+
23
+ .chat-container {
24
+ width: 400px;
25
+ height: 600px;
26
+ background: white;
27
+ border-radius: 15px;
28
+ box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
29
+ display: flex;
30
+ flex-direction: column;
31
+ overflow: hidden;
32
+ }
33
+
34
+ .chat-header {
35
+ background: #4a5568;
36
+ color: white;
37
+ padding: 15px 20px;
38
+ text-align: center;
39
+ }
40
+
41
+ .messages-container {
42
+ flex: 1;
43
+ padding: 20px;
44
+ overflow-y: auto;
45
+ background: #f7fafc;
46
+ }
47
+
48
+ .message {
49
+ margin-bottom: 15px;
50
+ padding: 10px 15px;
51
+ border-radius: 15px;
52
+ max-width: 80%;
53
+ word-wrap: break-word;
54
+ }
55
+
56
+ .message.own {
57
+ background: #4299e1;
58
+ color: white;
59
+ margin-left: auto;
60
+ border-bottom-right-radius: 5px;
61
+ }
62
+
63
+ .message.other {
64
+ background: #e2e8f0;
65
+ color: #2d3748;
66
+ border-bottom-left-radius: 5px;
67
+ }
68
+
69
+ .message-info {
70
+ font-size: 0.8em;
71
+ opacity: 0.7;
72
+ margin-bottom: 3px;
73
+ }
74
+
75
+ .input-container {
76
+ padding: 15px 20px;
77
+ background: white;
78
+ border-top: 1px solid #e2e8f0;
79
+ display: flex;
80
+ gap: 10px;
81
+ }
82
+
83
+ .message-input {
84
+ flex: 1;
85
+ padding: 12px 15px;
86
+ border: 2px solid #e2e8f0;
87
+ border-radius: 25px;
88
+ outline: none;
89
+ font-size: 14px;
90
+ transition: border-color 0.3s;
91
+ }
92
+
93
+ .message-input:focus {
94
+ border-color: #4299e1;
95
+ }
96
+
97
+ .send-button {
98
+ padding: 12px 25px;
99
+ background: #4299e1;
100
+ color: white;
101
+ border: none;
102
+ border-radius: 25px;
103
+ cursor: pointer;
104
+ font-weight: bold;
105
+ transition: background 0.3s;
106
+ }
107
+
108
+ .send-button:hover {
109
+ background: #3182ce;
110
+ }
111
+
112
+ .send-button:disabled {
113
+ background: #a0aec0;
114
+ cursor: not-allowed;
115
+ }
116
+
117
+ .typing-indicator {
118
+ padding: 10px 15px;
119
+ font-style: italic;
120
+ color: #718096;
121
+ font-size: 0.9em;
122
+ }
123
+
124
+ /* Стили для скроллбара */
125
+ .messages-container::-webkit-scrollbar {
126
+ width: 6px;
127
+ }
128
+
129
+ .messages-container::-webkit-scrollbar-track {
130
+ background: #f1f1f1;
131
+ }
132
+
133
+ .messages-container::-webkit-scrollbar-thumb {
134
+ background: #cbd5e0;
135
+ border-radius: 3px;
136
+ }
137
+
138
+ .messages-container::-webkit-scrollbar-thumb:hover {
139
+ background: #a0aec0;
140
+ }
141
+ </style>
142
+ </head>
143
+ <body>
144
+ <div class="chat-container">
145
+ <div class="chat-header">
146
+ <h2>💬 Simple Chat</h2>
147
+ <small>Ваш ID: <span id="user-id">{{ user_id }}</span></small>
148
+ </div>
149
+
150
+ <div class="messages-container" id="messages-container">
151
+ <div class="typing-indicator" id="welcome-message">
152
+ Добро пожаловать в чат! Начните общение...
153
+ </div>
154
+ </div>
155
+
156
+ <div class="input-container">
157
+ <input type="text"
158
+ class="message-input"
159
+ id="message-input"
160
+ placeholder="Введите сообщение..."
161
+ maxlength="500">
162
+ <button class="send-button" id="send-button">Отправить</button>
163
+ </div>
164
+ </div>
165
+
166
+ <script>
167
+ class Chat {
168
+ constructor() {
169
+ this.userId = document.getElementById('user-id').textContent;
170
+ this.messagesContainer = document.getElementById('messages-container');
171
+ this.messageInput = document.getElementById('message-input');
172
+ this.sendButton = document.getElementById('send-button');
173
+ this.lastMessageId = 0;
174
+
175
+ this.init();
176
+ }
177
+
178
+ init() {
179
+ // Загружаем историю сообщений при загрузке
180
+ this.loadMessages();
181
+
182
+ // Настраиваем обработчики событий
183
+ this.sendButton.addEventListener('click', () => this.sendMessage());
184
+ this.messageInput.addEventListener('keypress', (e) => {
185
+ if (e.key === 'Enter') {
186
+ this.sendMessage();
187
+ }
188
+ });
189
+
190
+ // Обновляем сообщения каждые 2 секунды
191
+ setInterval(() => this.loadNewMessages(), 2000);
192
+
193
+ // Автофокус на поле ввода
194
+ this.messageInput.focus();
195
+ }
196
+
197
+ async sendMessage() {
198
+ const message = this.messageInput.value.trim();
199
+
200
+ if (!message) return;
201
+
202
+ // Блокируем кнопку во время отправки
203
+ this.sendButton.disabled = true;
204
+ this.sendButton.textContent = 'Отправка...';
205
+
206
+ try {
207
+ const response = await fetch('/send_message', {
208
+ method: 'POST',
209
+ headers: {
210
+ 'Content-Type': 'application/json',
211
+ },
212
+ body: JSON.stringify({
213
+ user_id: this.userId,
214
+ message: message
215
+ })
216
+ });
217
+
218
+ const result = await response.json();
219
+
220
+ if (result.status === 'success') {
221
+ this.messageInput.value = '';
222
+ this.loadNewMessages(); // Обновляем сообщения сразу после отправки
223
+ } else {
224
+ alert('Ошибка отправки: ' + result.message);
225
+ }
226
+ } catch (error) {
227
+ console.error('Ошибка:', error);
228
+ alert('Ошибка соединения с сервером');
229
+ } finally {
230
+ this.sendButton.disabled = false;
231
+ this.sendButton.textContent = 'Отправить';
232
+ }
233
+ }
234
+
235
+ async loadMessages() {
236
+ try {
237
+ const response = await fetch('/get_messages');
238
+ const data = await response.json();
239
+
240
+ this.displayMessages(data.messages);
241
+ this.scrollToBottom();
242
+ } catch (error) {
243
+ console.error('Ошибка загрузки сообщений:', error);
244
+ }
245
+ }
246
+
247
+ async loadNewMessages() {
248
+ try {
249
+ const response = await fetch(`/get_new_messages?last_id=${this.lastMessageId}`);
250
+ const data = await response.json();
251
+
252
+ if (data.messages.length > 0) {
253
+ this.displayMessages(data.messages, false);
254
+ this.scrollToBottom();
255
+ }
256
+ } catch (error) {
257
+ console.error('Ошибка загрузки новых сообщений:', error);
258
+ }
259
+ }
260
+
261
+ displayMessages(messages, clear = true) {
262
+ if (clear) {
263
+ // Убираем приветственное сообщение
264
+ const welcomeMsg = document.getElementById('welcome-message');
265
+ if (welcomeMsg) welcomeMsg.remove();
266
+
267
+ this.messagesContainer.innerHTML = '';
268
+ }
269
+
270
+ messages.forEach(message => {
271
+ if (message.id > this.lastMessageId) {
272
+ this.lastMessageId = message.id;
273
+ }
274
+
275
+ const messageElement = document.createElement('div');
276
+ messageElement.className = `message ${message.user_id === this.userId ? 'own' : 'other'}`;
277
+
278
+ messageElement.innerHTML = `
279
+ <div class="message-info">
280
+ ${message.username} • ${message.timestamp}
281
+ </div>
282
+ <div class="message-text">${this.escapeHtml(message.text)}</div>
283
+ `;
284
+
285
+ this.messagesContainer.appendChild(messageElement);
286
+ });
287
+ }
288
+
289
+ scrollToBottom() {
290
+ this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
291
+ }
292
+
293
+ escapeHtml(text) {
294
+ const div = document.createElement('div');
295
+ div.textContent = text;
296
+ return div.innerHTML;
297
+ }
298
+ }
299
+
300
+ // Инициализация чата после загрузки страницы
301
+ document.addEventListener('DOMContentLoaded', () => {
302
+ new Chat();
303
+ });
304
+ </script>
305
+ </body>
306
+ </html>