Пример #1
0
    public void Handle(Agent agent, ReadPacket rp)
    {
        byte action = rp.ReadInt8();
        long aid    = rp.ReadInt64();

        agent.Action(aid, action, rp);
    }
Пример #2
0
    //处理服务端消息
    void onMessage(WebSocket ws, byte[] message)
    {
        ReadPacket rp = PacketFunc.CreateReadPacket(message);
        //包头
        byte t = rp.ReadInt8();

        if (t == MsgOutsideDef.MsgNull)
        {
            return;
        }

        //发送者ID,广播为0
        long clientID = rp.ReadInt64();

        switch (t)
        {
        case MsgOutsideDef.MsgPeer:      //单播消息
            short gameType = rp.ReadInt16();
            foreach (Command cmd in responseCommands)
            {
                if (cmd.Type() == gameType)
                {
                    cmd.Handle(this, rp);
                    break;
                }
            }
            break;

        case MsgOutsideDef.MsgBroadcast:      //广播消息
            byte _ = rp.ReadInt8();
            gameType = rp.ReadInt16();
            foreach (Command cmd in responseCommands)
            {
                if (cmd.Type() == gameType)
                {
                    cmd.Handle(this, rp);
                    break;
                }
            }
            break;

        case MsgOutsideDef.MsgRoomJoin:      //加入房间
            long joinID = rp.ReadInt64();
            bool canAdd = true;
            foreach (var id in ids)
            {
                if (id == joinID)
                {
                    canAdd = false;
                    break;
                }
            }
            if (canAdd)
            {
                ids.Add(joinID);
                if (statusCallBack != null)
                {
                    statusCallBack(NetStatusType.Join, this, joinID);
                }
            }
            Debug.Log("Room join id: " + joinID.ToString());
            break;

        case MsgOutsideDef.MsgRoomLeave:      //离开房间
            long leaveID = rp.ReadInt64();
            ids.Remove(leaveID);
            if (statusCallBack != null)
            {
                statusCallBack(NetStatusType.Leave, this, leaveID);
            }
            Debug.Log("Room leave id: " + leaveID.ToString());
            break;

        case MsgOutsideDef.MsgRoomIds:      //获得房间ID列表
            myID = rp.ReadInt64();
            Debug.Log("myID: " + myID.ToString());
            ids = new List <long>();
            ids.Add(myID);
            long otherID;
            while ((otherID = rp.ReadInt64()) != 0)
            {
                ids.Add(otherID);
            }
            if (statusCallBack != null)
            {
                statusCallBack(NetStatusType.Ids, this);
            }
            Debug.Log("Room id count: " + ids.ToArray().Length.ToString());
            break;
        }
    }