from aiohttp import web import aiohttp connections async def websocket

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from aiohttp import web
import aiohttp
connections = []
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
connections.append(ws)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
else:
for connection in connections:
await connection.send_str("123123")
print(connections)
return ws
async def index(request):
return web.FileResponse('test.html')
app = web.Application()
app.router.add_get('/ws', websocket_handler)
app.router.add_get('/', index)
web.run_app(app)