add websocket_handler and time_response

Signed-off-by: kirill <kirill.milkin05@mail.ru>
pull/3/head
kirill 2 years ago
parent a7eee835b8
commit a946dd42c8
  1. 43
      demo/views/frontend.py

@ -15,6 +15,8 @@ async def time_response(ws: WebSocketResponse, delay: int = 60):
await asyncio.sleep(delay)
except ConnectionResetError:
await asyncio.sleep(delay)
@template('index.html')
async def index(request):
return {}
@ -24,28 +26,31 @@ async def index_2(request):
return {}
async def websocket_handler(request):
ws: WebSocketResponse = WebSocketResponse()
await ws.prepare(request)
asyncio.create_task(time_response(ws))
async for msg in ws:
ws: WebSocketResponse = WebSocketResponse()
await ws.prepare(request)
asyncio.create_task(time_response(ws))
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
else:
await ws.send_str(f'Вы послали: {msg.data}')
elif msg.type == WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
if msg.type == aiohttp.WSMsgType.TEXT:
print('websocket connection closed')
if msg.data == 'close':
await ws.close()
else:
await ws.send_str(f'Вы послали: {msg.data}')
elif msg.type == WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
return ws
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 Response(text=response_text)
data = await request.post()
text = data.get('text')
response_text = f'Вы ввели: {text}'
print(response_text)
return Response(text=response_text)
Loading…
Cancel
Save