Ejemplo n.º 1
0
        /// <summary>
        /// 根据服务器名称获取并发送给客户端 ip和端口
        /// </summary>
        /// <param name="serverName">服务器名称</param>
        /// <param name="messageNum">收到的消息号</param>
        /// <param name="session"></param>
        /// <param name="status">状态 1为正常切换服务器,2为有牌局为结束</param>
        /// <returns></returns>
        public static bool GetServerIP(string serverName, int messageNum, GameSession session, int status, UserInfo userinfo)
        {
            XmlDocument       doc      = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreComments = true;//忽略文档里面的注释
            string ip   = string.Empty;
            string port = string.Empty;

            using (var reader = XmlReader.Create(@"ServerNameList.xml", settings))
            {
                doc.Load(reader);
                XmlNode nodeList = doc.SelectSingleNode("servers");
                foreach (XmlNode item in nodeList.ChildNodes)
                {
                    XmlElement xe = (XmlElement)item;
                    if (xe.GetAttribute("name").Equals(serverName))
                    {
                        ip   = xe.ChildNodes[0].InnerText;
                        port = xe.ChildNodes[1].InnerText;
                    }
                }
            }
            if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))
            {
                session.Logger.Error(string.Format("服务器配置文件不存在 {0} 服务器", serverName));
                XmlNode nodeList = doc.SelectSingleNode("servers");
                foreach (XmlNode item in nodeList.ChildNodes)
                {
                    XmlElement xe = (XmlElement)item;
                    if (xe.GetAttribute("name").Equals(GameInformationBase.DEFAULTGAMESERVERNAME))
                    {
                        ip   = xe.ChildNodes[0].InnerText;
                        port = xe.ChildNodes[1].InnerText;
                    }
                }
                GetClient().KeyDelete(RedisUtility.GetKey(GameInformationBase.COMMUNITYUSERGAME, userinfo.openid, userinfo.unionid));
            }
            ReturnServerIP.Builder serverIP = ReturnServerIP.CreateBuilder();
            serverIP.SetIp(ip).SetPort(port).SetStatus(status);
            byte[] serverIPData = serverIP.Build().ToByteArray();
            return(session.TrySend(new ArraySegment <byte>(CreateHead.CreateMessage(GameInformationBase.BASEAGREEMENTNUMBER + 1010, serverIPData.Length, messageNum, serverIPData))));
        }
Ejemplo n.º 2
0
        protected override void OnSessionClosed(CloseReason reason)
        {
            //add you logics which will be executed after the session is closed
            base.OnSessionClosed(reason);
            if (Gongyong.userlist.Count == 0)
            {
                return;
            }
            var info = Gongyong.userlist.FirstOrDefault(w => w.session.SessionID.Equals(this.SessionID));

            if (info != null)
            {
                if (Gongyong.mulist.Count == 0)
                {
                    return;
                }
                var muInfo = Gongyong.mulist.FirstOrDefault(w => w.Openid.Equals(info.openid));
                if (muInfo != null)
                {
                    var room = Gongyong.roomlist.FirstOrDefault(u => u.RoomID == muInfo.RoomID);
                    if (room != null && room.startgame == 0)
                    {
                        RedisUtility.Remove(RedisUtility.GetKey(GameInformationBase.COMMUNITYUSERGAME, info.openid, info.unionid));
                        // RedisUtility.Remove(RedisUtility.GetKey(GameInformationBase.COMMUNITYUSERINFO, info.openid, info.unionid));
                        UserExitLogic exit = new UserExitLogic();
                        //Gongyong.mulist.Remove(muInfo);
                        if (muInfo.IsHomeowner)
                        {
                            Gongyong.userlist.Remove(info);
                            exit.DissolutionRoom(room, 0, 2);
                        }
                        else
                        {
                            Gongyong.userlist.Remove(info);
                            exit.UserExit(muInfo, room.RoomID, info.openid, 0, null, true);
                        }
                    }
                }
            }
        }