from aiohttp import web WSMsgType class BaseHandler def __init__ self

 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
47
48
49
50
51
from aiohttp import web, WSMsgType
class BaseHandler:
def __init__(self):
self.ws = web.WebSocketResponse()
async def websocket_handler(self, request):
ws = self.ws
await ws.prepare(request)
await self.open()
async for msg in ws:
if msg.type == WSMsgType.TEXT:
if msg.data == 'close':
await self.on_close()
await ws.close()
else:
if await self.on_message(msg.data):
await ws.send_str(msg.data + '/answer')
elif msg.type == WSMsgType.ERROR:
await self.on_close()
print('ws connection closed with exception %s' %
ws.exception())
await self.on_close()
print('websocket connection closed')
return ws
class BaseHandler(BaseHandler):
async def open(self):
print("hello marat")
async def on_message(self, msg):
print("Napisali hyiny" + msg)
async def on_close(self):
print("Zakrito")
b = BaseHandler()
app = web.Application()
app.add_routes([web.get('/', b.websocket_handler)])
if __name__ == '__main__':
web.run_app(app)