def websocket(req):
ws = web.WebSocketResponse()
await ws.prepare(req)
async for msg in ws:
if msg.type != WSMsgType.TEXT: continue
try:
msg = json.loads(msg.data)
if msg["type"] != "login": continue
room = msg["data"]["room"][:32]
if room: break
except (ValueError, KeyError, TypeError):
# Any parsing error, just wait for another message
continue
else:
# Something went wrong with the handshake. Kick
# the client and let them reconnect.
await ws.close()
return ws
if room not in rooms: Room(room)
return await rooms[room].websocket(ws, msg["data"])
# After all the custom routes, handle everything else by loading static files.
评论列表
文章目录