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)