Compare commits
No commits in common. '1f334dd462514f2f6e439dc01c309eb2a86d07d0' and 'aaf608d2d2ceac2d7c1a9f1e14e601cfcd32a923' have entirely different histories.
1f334dd462
...
aaf608d2d2
@ -1 +0,0 @@ |
|||||||
from . import secondary_functions |
|
@ -1,21 +0,0 @@ |
|||||||
import time |
|
||||||
import asyncio |
|
||||||
from aiohttp.web_ws import WebSocketResponse |
|
||||||
|
|
||||||
|
|
||||||
async def time_response(ws: WebSocketResponse, delay: int = 60): |
|
||||||
while True: |
|
||||||
new_time: time.gmtime = time.gmtime(time.time()) |
|
||||||
hour = new_time.tm_hour + 3 |
|
||||||
if hour >= 24: |
|
||||||
hour = hour - 24 |
|
||||||
minutes = new_time.tm_min |
|
||||||
if minutes < 10: |
|
||||||
minutes = f'0{minutes}' |
|
||||||
result_response = f'Время: {hour}:{minutes}' |
|
||||||
try: |
|
||||||
await ws.send_str(result_response) |
|
||||||
await asyncio.sleep(delay) |
|
||||||
except ConnectionResetError: |
|
||||||
break |
|
||||||
|
|
@ -1,92 +0,0 @@ |
|||||||
<!DOCTYPE html> |
|
||||||
<html lang="en"> |
|
||||||
<head> |
|
||||||
<meta charset="UTF-8" /> |
|
||||||
<title>Знакомство с aiohttp</title> |
|
||||||
<style> |
|
||||||
html { |
|
||||||
font-family: Verdana, "sans-serif"; |
|
||||||
} |
|
||||||
|
|
||||||
body { |
|
||||||
min-height: 100vh; |
|
||||||
background: linear-gradient( |
|
||||||
90deg, |
|
||||||
rgba(2, 0, 36, 1) 0%, |
|
||||||
rgba(9, 9, 121, 1) 35%, |
|
||||||
rgba(0, 212, 255, 1) 100% |
|
||||||
); |
|
||||||
} |
|
||||||
h1, |
|
||||||
p { |
|
||||||
color: white; |
|
||||||
} |
|
||||||
.time_block, |
|
||||||
.output_block { |
|
||||||
border: 2px solid white; |
|
||||||
border-radius: 5px; |
|
||||||
margin: 10px; |
|
||||||
padding: 10px; |
|
||||||
height: 50px; |
|
||||||
width: 300px; |
|
||||||
} |
|
||||||
.message_form { |
|
||||||
margin: 10px; |
|
||||||
} |
|
||||||
</style> |
|
||||||
</head> |
|
||||||
<body> |
|
||||||
<header> |
|
||||||
<h1>Дата и время</h1> |
|
||||||
</header> |
|
||||||
<main> |
|
||||||
<div class="time_block"> |
|
||||||
<p id="time"></p> |
|
||||||
</div> |
|
||||||
<div class="message_form"> |
|
||||||
<form id="myForm2"> |
|
||||||
<input |
|
||||||
type="text" |
|
||||||
name="message_text_user" |
|
||||||
placeholder="Введите текст" |
|
||||||
/> |
|
||||||
<input type="button" name="submit_message" value="Отправить" /> |
|
||||||
</form> |
|
||||||
</div> |
|
||||||
<div class="output_block"> |
|
||||||
<p id="output_message" class="output"></p> |
|
||||||
</div> |
|
||||||
</main> |
|
||||||
<script> |
|
||||||
const messageTime = document.getElementById("time"); |
|
||||||
const messageInput = document.querySelector("[name=message_text_user]"); |
|
||||||
const messageOtput = document.getElementById("output_message"); |
|
||||||
const sendMessage = document.querySelector("[name=submit_message]"); |
|
||||||
let websocketClient = new WebSocket("ws://127.0.0.1:8080/ws"); |
|
||||||
websocketClient.onopen = () => { |
|
||||||
console.log("Client connected!"); |
|
||||||
sendMessage.onclick = () => { |
|
||||||
console.log("1"); |
|
||||||
websocketClient.send(messageInput.value); |
|
||||||
messageInput.value = ""; |
|
||||||
}; |
|
||||||
}; |
|
||||||
|
|
||||||
websocketClient.onmessage = (message) => { |
|
||||||
if (message.data.includes("Время: ")) { |
|
||||||
if (typeof messageTime.textContent !== "undefined") { |
|
||||||
messageTime.textContent = message.data; |
|
||||||
} else { |
|
||||||
messageTime.innerText = message.data; |
|
||||||
} |
|
||||||
} else { |
|
||||||
if (typeof messageOtput.textContent !== "undefined") { |
|
||||||
messageOtput.textContent = message.data; |
|
||||||
} else { |
|
||||||
messageOtput.innerText = message.data; |
|
||||||
} |
|
||||||
} |
|
||||||
}; |
|
||||||
</script> |
|
||||||
</body> |
|
||||||
</html> |
|
@ -1,38 +1,16 @@ |
|||||||
import asyncio |
|
||||||
import aiohttp |
import aiohttp |
||||||
from aiohttp import WSMsgType |
|
||||||
from aiohttp.web_response import Response |
|
||||||
from aiohttp.web_ws import WebSocketResponse |
|
||||||
from aiohttp_jinja2 import template |
from aiohttp_jinja2 import template |
||||||
from ..func import secondary_functions |
|
||||||
|
|
||||||
|
|
||||||
@template('index.html') |
@template('index.html') |
||||||
async def index(request): |
async def index(request): |
||||||
return {} |
return {} |
||||||
|
|
||||||
@template('index_2.html') |
|
||||||
async def index_2(request): |
|
||||||
return {} |
|
||||||
|
|
||||||
|
|
||||||
async def websocket_handler(request): |
|
||||||
ws: WebSocketResponse = WebSocketResponse() |
|
||||||
await ws.prepare(request) |
|
||||||
asyncio.create_task(secondary_functions.time_response(ws, delay=60)) |
|
||||||
async for msg in ws: |
|
||||||
if msg.type == aiohttp.WSMsgType.TEXT: |
|
||||||
await ws.send_str(f'Вы ввели: {msg.data}') |
|
||||||
elif msg.type == WSMsgType.ERROR: |
|
||||||
print('ws connection closed with exception %s' % |
|
||||||
ws.exception()) |
|
||||||
print('websocket connection closed') |
|
||||||
return ws |
|
||||||
|
|
||||||
|
|
||||||
async def button_clicked(request): |
async def button_clicked(request): |
||||||
data = await request.post() |
data = await request.post() |
||||||
text = data.get('text') |
text = data.get('text') |
||||||
response_text = f'Вы ввели: {text}' |
response_text = f'Вы ввели: {text}' |
||||||
print(response_text) |
print(response_text) |
||||||
return Response(text=response_text) |
return aiohttp.web.Response(text=response_text) |
Loading…
Reference in new issue