def client_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
app = request.app
log.info("Client connected.")
clients = app['clients']
clients.add(ws)
notify_state(app, specific_client=ws)
try:
async for raw_msg in ws:
if raw_msg.tp == aiohttp.MsgType.text:
msg = api.Message.deserialize(raw_msg.data)
log.info("User message: %s", msg)
await handle_user_message(app, ws, msg)
elif raw_msg.tp == aiohttp.MsgType.closed:
break
elif raw_msg.tp == aiohttp.MsgType.error:
log.info("User websocket error: %s", raw_msg)
break
else:
log.error("Unknown user message type: %s, ignoring.",
raw_msg.tp)
finally:
log.info("Client connection closed.")
clients.remove(ws)
return ws
评论列表
文章目录