Compare commits

..

No commits in common. '3dbb0257ae88fa10033e7216e44797664dac98e8' and '7fbadce5587928a245e446d57b9444080cfb7e28' have entirely different histories.

  1. 1
      demo/func/__init__.py
  2. 20
      demo/func/secondary_functions.py
  3. 3
      demo/routes.py
  4. 61
      demo/views/frontend.py

@ -1 +0,0 @@
from . import secondary_functions

@ -1,20 +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:
await asyncio.sleep(delay)

@ -6,6 +6,7 @@ def setup_routes(app):
app.router.add_routes([aiohttp.web.get('/index.html', frontend.index), app.router.add_routes([aiohttp.web.get('/index.html', frontend.index),
aiohttp.web.post('/submit', frontend.button_clicked), aiohttp.web.post('/submit', frontend.button_clicked),
aiohttp.web.get('/index_2.html', frontend.index_2), aiohttp.web.get('/index_2.html', frontend.index_2),
aiohttp.web.get('/ws', frontend.websocket_handler),] aiohttp.web.get('/ws', frontend.websocket_handler)
]
) )

@ -1,10 +1,20 @@
import asyncio
import aiohttp import aiohttp
from aiohttp import WSMsgType from aiohttp import WSMsgType
from aiohttp.web_response import Response from aiohttp.web_response import Response
from aiohttp.web_ws import WebSocketResponse from aiohttp.web_ws import WebSocketResponse
from aiohttp_jinja2 import template from aiohttp_jinja2 import template
from ..func import secondary_functions import time
import asyncio
async def time_response(ws: WebSocketResponse, delay: int = 60):
while True:
new_time: time.gmtime = time.gmtime(time.time())
time_response = f'Время: {new_time.tm_hour + 3}:{new_time.tm_min} Задержка: {delay}'
try:
await ws.send_str(time_response)
await asyncio.sleep(delay)
except ConnectionResetError:
await asyncio.sleep(delay)
@template('index.html') @template('index.html')
@ -16,30 +26,31 @@ async def index_2(request):
return {} return {}
async def websocket_handler(request): async def websocket_handler(request):
ws: WebSocketResponse = WebSocketResponse() ws: WebSocketResponse = WebSocketResponse()
await ws.prepare(request) await ws.prepare(request)
asyncio.create_task(secondary_functions.time_response(ws, delay=60)) asyncio.create_task(time_response(ws))
async for msg in ws: async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close': if msg.data == 'close':
await ws.close() await ws.close()
else: else:
await ws.send_str(f'Вы ввели: {msg.data}') await ws.send_str(f'Вы послали: {msg.data}')
elif msg.type == WSMsgType.ERROR: elif msg.type == WSMsgType.ERROR:
print('ws connection closed with exception %s' % print('ws connection closed with exception %s' %
ws.exception()) ws.exception())
print('websocket connection closed') print('websocket connection closed')
return ws 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 Response(text=response_text)
Loading…
Cancel
Save