Compare commits
13 Commits
aaf608d2d2
...
1f334dd462
| Author | SHA1 | Date |
|---|---|---|
|
|
1f334dd462 | 3 years ago |
|
|
5537418968 | 3 years ago |
|
|
8e981e28f9 | 3 years ago |
|
|
3dbb0257ae | 3 years ago |
|
|
2a0189e892 | 3 years ago |
|
|
5edc3beb78 | 3 years ago |
|
|
7fbadce558 | 3 years ago |
|
|
a946dd42c8 | 3 years ago |
|
|
a7eee835b8 | 3 years ago |
|
|
bef3d2fc94 | 3 years ago |
|
|
aa95400309 | 3 years ago |
|
|
f6627d3d26 | 3 years ago |
|
|
c95a7e4c58 | 3 years ago |
@ -0,0 +1 @@
|
||||
from . import secondary_functions
|
||||
@ -0,0 +1,21 @@
|
||||
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 +1,2 @@
|
||||
from . import frontend
|
||||
|
||||
|
||||
@ -1,16 +1,38 @@
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
from aiohttp import WSMsgType
|
||||
from aiohttp.web_response import Response
|
||||
from aiohttp.web_ws import WebSocketResponse
|
||||
from aiohttp_jinja2 import template
|
||||
from ..func import secondary_functions
|
||||
|
||||
|
||||
@template('index.html')
|
||||
async def index(request):
|
||||
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):
|
||||
data = await request.post()
|
||||
text = data.get('text')
|
||||
response_text = f'Вы ввели: {text}'
|
||||
print(response_text)
|
||||
return aiohttp.web.Response(text=response_text)
|
||||
return Response(text=response_text)
|
||||
Loading…
Reference in new issue