50 lines
1.5 KiB
HTML
50 lines
1.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<title>WebSocket Client Test</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; margin: 20px; }
|
|
#messages { border: 1px solid #ccc; height: 300px; overflow-y: scroll; padding: 10px; }
|
|
#status { margin-bottom: 10px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<h2>WebSocket Client</h2>
|
|
<div id="status">Статус: <span id="connection-status">Отключено</span></div>
|
|
<div id="messages"></div>
|
|
|
|
<script>
|
|
const statusElem = document.getElementById('connection-status');
|
|
const messagesElem = document.getElementById('messages');
|
|
const wsProtocol = location.protocol === 'https:' ? 'wss' : 'ws';
|
|
const wsUrl = `${wsProtocol}://${location.host}/ws/client/`;
|
|
const socket = new WebSocket(wsUrl);
|
|
|
|
socket.onopen = () => {
|
|
statusElem.textContent = 'Подключено';
|
|
statusElem.style.color = 'green';
|
|
};
|
|
|
|
socket.onmessage = event => {
|
|
const data = event.data;
|
|
const msgElem = document.createElement('div');
|
|
msgElem.textContent = `Получено: ${data}`;
|
|
messagesElem.appendChild(msgElem);
|
|
messagesElem.scrollTop = messagesElem.scrollHeight;
|
|
};
|
|
|
|
socket.onclose = () => {
|
|
statusElem.textContent = 'Отключено';
|
|
statusElem.style.color = 'red';
|
|
};
|
|
|
|
socket.onerror = error => {
|
|
console.error('WebSocket ошибка:', error);
|
|
};
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|