You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

import asyncio
2 years ago
import aiohttp
from aiohttp import WSMsgType
from aiohttp.web_response import Response
from aiohttp.web_ws import WebSocketResponse
2 years ago
from aiohttp_jinja2 import template
from ..func import secondary_functions
2 years ago
@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:
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())
print('websocket connection closed')
return ws
2 years ago
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)