async def onData(self, ws, gamePacket): """ 收到ws数据包时回调 """ log.info('ws received:' + gamePacket.dump() + ' ws:' + str(ws)) protocol = gamePacket.protocol roomId = ws_connects_mgr.getRoomId(ws) uid = ws_connects_mgr.getUid(ws) room = room_manager.get_room(roomId) log.info("handle uid:{0} protocol:{1} of room:{2}".format( uid, protocol, room.roomId if room else '')) if not protocol or not room: return # 根据客户端的包,来执行相应的操作 if protocol == PROTOCOL.C2SHeartbeat: # 游戏心跳包 # 原样返回 await ws_channel_util.write( ws, GamePacket(PROTOCOL.S2CHeartbeat, gamePacket.payload)) elif protocol == PROTOCOL.C2SSignup: # 加入游戏 room.signup(uid, json.loads(gamePacket.payload)) elif protocol == PROTOCOL.C2SStart: # 开始游戏 room.start(uid) elif protocol == PROTOCOL.C2SGameover: # 主播主动结束游戏 room.gameOver(uid) else: log.warn("protocol invalid:{0},{1}".format(protocol), gamePacket.dump()) pass pass
async def onConnected(self, ws, roomId, uid, isPresenter, pkRoomId=None, profileId=None): """ 新建ws连接回调 """ log.info('ws new:' + ',roomid:' + roomId + ',pkRoomId:' + (pkRoomId if pkRoomId else '') + ',isPresenter:' + str(isPresenter) + ',uid:' + uid + ' ws:' + str(ws)) # 如果是主播 if isPresenter: if not pkRoomId: pkRoomId = roomId else: # 观众需要根据主播id去查一下 pkRoomId = room_manager.get_pkroom_of_presenter(profileId) if not pkRoomId: # 观众连接,返回房间关闭 await ws_channel_util.write(ws, GamePacket(PROTOCOL.S2CRoomClosed)) await ws.close() # 查询pk房间 room = room_manager.get_room(pkRoomId) # 房间不存在 if not room: # 如果是主播,且pk房间号是自己的房间号,创建房间 if isPresenter and pkRoomId == roomId: await ws_connects_mgr.onLogin(uid, pkRoomId, ws) room = room_manager.new_room(pkRoomId, uid) await room.onJoin(uid, isPresenter=isPresenter) else: # 非pk房主主播,或者观众连接,返回房间关闭 await ws_channel_util.write(ws, GamePacket(PROTOCOL.S2CRoomClosed)) await ws.close() else: # 房间存在 # 加入房间 await ws_connects_mgr.onLogin(uid, pkRoomId, ws) await room.onJoin(uid, roomId, isPresenter=isPresenter) # 是主播 if isPresenter: room_manager.presenter_join_pkroom(pkRoomId, uid)
async def onConnected(self, ws, roomId, uid, isPresenter): """ 新建ws连接回调 """ log.info('ws new:' + ',roomid:' + roomId + ',uid:' + uid + ' ws:' + str(ws)) room = room_manager.get_room(roomId) # 房间不存在 if not room: # 如果是主播,创建房间 if isPresenter: await ws_connects_mgr.onLogin(uid, roomId, ws) room = await room_manager.new_room(roomId, uid) await room.onJoin(uid) else: # 观众连接,返回房间关闭 await ws_channel_util.write(ws, GamePacket(PROTOCOL.S2CRoomClosed)) await ws.close() else: # 加入房间 await ws_connects_mgr.onLogin(uid, roomId, ws) await room.onJoin(uid)