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 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 onDisconnected(self, ws): """ ws连接断开 """ log.info('ws closed:' + str(ws)) ws_connects_mgr.onLogout(ws) if not ws.closed: await ws_channel_util.write(ws, GamePacket(PROTOCOL.S2CRoomClosed)) await ws.close() pass
async def broadcast(roomId, uri, data, allUids, exUid=None): """ 房间内广播数据的方法,exUid为要排除的用户id列表 """ game_packet = GamePacket(uri, data) for uid in allUids: is_exclude = False # 是否要排除当前uid if exUid and isinstance(exUid, list) or isinstance(exUid, tuple): if exUid.__contains__(uid): is_exclude = True elif exUid and exUid == uid: is_exclude = True else: is_exclude = False if not is_exclude: await unicast_packet(roomId, uid, game_packet)
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)
async def unicast(roomId, uid, uri, data): """ 单播方法,给房间内指定用户发送消息 """ game_packet = GamePacket(uri, data) ws = ws_connects_mgr.getWs(uid, roomId) await write(ws, game_packet)