use secondary_functions in ws_handler

Signed-off-by: kirill <kirill.milkin05@mail.ru>
pull/3/head
kirill 2 years ago
parent 5edc3beb78
commit 2a0189e892
  1. 19
      demo/views/frontend.py

@ -1,20 +1,10 @@
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
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)
from ..func import secondary_functions
@template('index.html')
@ -26,11 +16,10 @@ async def index_2(request):
return {}
async def websocket_handler(request):
ws: WebSocketResponse = WebSocketResponse()
await ws.prepare(request)
asyncio.create_task(time_response(ws))
asyncio.create_task(secondary_functions.time_response(ws, delay=60))
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
@ -38,7 +27,7 @@ async def websocket_handler(request):
if msg.data == 'close':
await ws.close()
else:
await ws.send_str(f'Вы послали: {msg.data}')
await ws.send_str(f'Вы ввели: {msg.data}')
elif msg.type == WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())

Loading…
Cancel
Save