class Game connections set async def broadcast self message_to_broadca

 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
class Game:
connections = set()
async def broadcast(self, message_to_broadcast):
async for connection in self.connections:
await connection.send_json(message_to_broadcast)
async def do_the_shit(self, msg):
await self.broadcast({'ty':'pidor'})
async def websocket_handler(self, request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
else:
self.connections.add(ws)
await ws.send_json(await self.do_the_shit(msg.data))
elif msg.type == WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
print('websocket connection closed')
return ws