blob: c501025eedea4fc3d01d7fbbc1bd56050d919fd6 (
plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
from logging import getLogger
from aiohttp import web
class WSRouter:
def __init__(self) -> None:
self.ws = None
self.routes = {}
self.logger = getLogger("WSRouter")
async def add_routes(self, routes):
self.routes.update(routes)
async def handle(self, request):
self.logger.debug('Websocket connection starting')
ws = web.WebSocketResponse()
await ws.prepare(request)
self.logger.debug('Websocket connection ready')
self.ws = ws
try:
async for msg in ws:
self.logger.debug(msg)
if msg.type == aiohttp.WSMsgType.TEXT:
self.logger.debug(msg.data)
if msg.data == 'close':
# DO NOT RELY ON THIS!
break
else:
# do stuff with the message
data = msg.json()
if self.routes[data.route]:
res = await self.routes[data.route](data.data)
finally:
try:
await ws.close()
except:
pass
self.logger.debug('Websocket connection closed')
return ws
async def write(self, dta: Dict[str, any]):
await self.ws.send_json(dta)
|