Exemple #1
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: SearchUser, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo <IEnumerable <User> > result = null;

            try
            {
                //解析Json字符串
                var    json    = JsonDict.Parse(content);
                string account = json["Account"];

                result = BizManager.AutofacBootstrapper.Resolve <IUserBiz>().SearchUser(account);
            }
            catch (Exception ex)
            {
                result = new ResultInfo <IEnumerable <User> >(ex);
            }
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.SEARCH_USER_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #2
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: SignUp, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ArraySegmentWrapper segmentWrapper = null;
            ResultInfo <User>   result         = null;

            try
            {
                //解析Json字符串
                var    json  = JsonDict.Parse(content);
                string name  = json["Name"];
                string email = json["Email"];
                string pwd   = json["Pwd"];
                //数据库验证
                result = BizManager.AutofacBootstrapper.Resolve <IAuthBiz>().SignUp(name, email, pwd);
            }
            catch (Exception ex)
            {
                result = new ResultInfo <User>(ex);
            }

            //发送注册响应消息
            segmentWrapper = new ArraySegmentWrapper(Constants.SIGNUP_RESPONSE_KEY, JConverter.SerializeToBytes(result));
            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #3
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: Talk, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo <Message> result = null;
            long toUId = 0L;

            try
            {
                //解析Json字符串
                var  json    = JsonDict.Parse(content);
                long fromUId = json.GetLong("FromUId");
                toUId = json.GetLong("ToUId");
                string msg = json["Content"];

                result = BizManager.AutofacBootstrapper.Resolve <IMessageBiz>().Save(fromUId, toUId, msg);
            }
            catch (Exception ex)
            {
                result = new ResultInfo <Message>(ex);
            }

            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.TALK_RESPONSE_KEY, JConverter.SerializeToBytes(result));
            var s = session.AppServer.GetSessions(o => o.User?.Id == toUId).FirstOrDefault();

            session.SendData(s, segmentWrapper.Wrapper());
        }
Exemple #4
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="session"></param>
        /// <param name="segment"></param>
        internal void SendData(ChatSession session, ArraySegment <byte> segment)
        {
            try
            {
                int count            = segment.Count;
                int maxRequestLength = AppConfiger.AppCfg.CurrCC_Cfg.MaxRequestLength;
                if (count > maxRequestLength)
                {
                    int offset = 0;
                    while (offset < count)
                    {
                        session.Send(segment.Array, offset, Math.Min(maxRequestLength, count - offset));

                        offset += maxRequestLength;
                    }
                }
                else
                {
                    session.Send(segment);
                }
            }
            catch (Exception ex)
            {
                var json = new JsonDict();
                json.Add("SessionID", session.SessionID);
                json.Add("Msg", string.Format("{0} 发送数据异常,异常信息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
                ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.ERROR_RESPONSE_KEY, json.ToBytes());
                session.Send(segmentWrapper.Wrapper());
            }
        }
Exemple #5
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: SaveContacts, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo result = null;

            try
            {
                //解析Json字符串
                var    json       = JsonDict.Parse(content);
                long   oUserId    = json.GetLong("OUserId");
                long   cUserId    = json.GetLong("CUserId");
                string customName = json["CustomName"];

                result = BizManager.AutofacBootstrapper.Resolve <IContactsBiz>().Save(oUserId, cUserId, customName);
            }
            catch (Exception ex)
            {
                result = new ResultInfo(ex);
            }
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.SAVE_CONTACTS_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #6
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: OnlineList, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo <IEnumerable <User> > result = null;

            try
            {
                result = new ResultInfo <IEnumerable <User> >()
                {
                    RMsg = "获取在线用户列表"
                };
                result.RData = session.AppServer.GetSessions(o => o.User?.ConnectState == EConnectState.Online).Select(o => o.User);
            }
            catch (Exception ex)
            {
                result = new ResultInfo <IEnumerable <User> >(ex);
            }
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.ONLINE_LIST_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #7
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: EditProfile, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo result = null;

            try
            {
                //解析Json字符串
                var user = JConverter.Deserialize <User>(content);

                result = BizManager.AutofacBootstrapper.Resolve <IUserBiz>().UpdateUser(user);
            }
            catch (Exception ex)
            {
                result = new ResultInfo(ex);
            }

            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.EDIT_PROFILE_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #8
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: GetContacts, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo <IEnumerable <Contacts> > result = null;

            try
            {
                //解析Json字符串
                var    json     = JsonDict.Parse(content);
                long   oUserId  = json.GetLong("OUserId");
                string keyword  = json["Keyword"];
                int    pageNum  = json.GetInt("PageNum");
                int    pageSize = json.GetInt("PageSize");

                result = BizManager.AutofacBootstrapper.Resolve <IContactsBiz>().GetDatas(oUserId, keyword, keyword, keyword, keyword, keyword, pageNum, pageSize);
            }
            catch (Exception ex)
            {
                result = new ResultInfo <IEnumerable <Contacts> >(ex);
            }
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.GET_CONTACTS_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #9
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: DownloadAvatar, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo <Tuple <long, byte[]> > result = null;

            try
            {
                //解析Json字符串
                var  json   = JsonDict.Parse(content);
                long userId = json.GetLong("UserId");

                var result2 = BizManager.AutofacBootstrapper.Resolve <IUserBiz>().FindById(userId);
                if (result2.Success)
                {
                    if (!string.IsNullOrEmpty(result2.RData.Avatar))
                    {
                        byte[] data = null;
                        using (FileStream fs = new FileStream(result2.RData.Avatar, FileMode.Open))
                        {
                            data = new byte[fs.Length];
                            fs.Read(data, 0, data.Length);
                        }
                        result = new ResultInfo <Tuple <long, byte[]> >()
                        {
                            RMsg = "下载用户头像成功", RData = new Tuple <long, byte[]>(userId, data)
                        };
                    }
                    else
                    {
                        result = new ResultInfo <Tuple <long, byte[]> >()
                        {
                            RCode = "1001", RMsg = "用户尚未设置头像", Success = false
                        };
                    }
                }
                else
                {
                    result = new ResultInfo <Tuple <long, byte[]> >()
                    {
                        RCode = "1002", RMsg = "用户不存在,下载用户头像失败", Success = false
                    };
                }
            }
            catch (Exception ex)
            {
                result = new ResultInfo <Tuple <long, byte[]> >(ex);
            }

            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.DOWNLOAD_AVATAR_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #10
0
        protected override void HandleException(Exception ex)
        {
            this.Logger.ErrorFormat("应用程序异常: {0}", ex.Message);

            var json = new JsonDict();

            json.Add("SessionID", this.SessionID);
            json.Add("Msg", string.Format("{0} 应用程序异常,异常信息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.ERROR_RESPONSE_KEY, json.ToBytes());

            this.SendData(this, segmentWrapper.Wrapper());
        }
Exemple #11
0
        protected override void OnSessionStarted()
        {
            this.Logger.InfoFormat("客户端 {0}/{1} 连入", this.SessionID, this.RemoteEndPoint);

            ResultInfo result = new ResultInfo()
            {
                RMsg = string.Format("{0} 您已进入联网监控区域", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
            };
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.CONNECTED_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            this.SendData(this, segmentWrapper.Wrapper());
        }
Exemple #12
0
        /// <summary>
        /// 提醒上线
        /// </summary>
        /// <param name="sessionId"></param>
        /// <param name="userName"></param>
        internal void NotifyOnline(string sessionId, string userName)
        {
            var ss = AppServer.GetSessions(o => !o.SessionID.Equals(sessionId));

            ss.AsParallel().ForAll(s =>
            {
                var json = new JsonDict();
                json.Add("SessionID", sessionId);
                json.Add("UserName", userName);
                json.Add("Msg", string.Format("{0} 客户端上线", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.NOTIFY_ONLINE_RESPONSE_KEY, json.ToBytes());

                this.SendData(s, segmentWrapper.Wrapper());
            });
        }
Exemple #13
0
        public override void OnCommandExecuting(CommandExecutingContext commandContext)
        {
            var session = commandContext.Session as ChatSession;

            //If the session is not logged in, cancel the executing of the command
            if (session.User == null)
            {
                ResultInfo result = new ResultInfo()
                {
                    RMsg = $"未登陆,命令执行失败。"
                };
                ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.ERROR_PERMIT_RESPONSE_KEY, JConverter.SerializeToBytes(result));
                session.Send(segmentWrapper.Wrapper());

                session.Close();
                commandContext.Cancel = true;
            }
        }
Exemple #14
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: SignIn, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);

            session.Logger.DebugFormat("消息内容:{0}", content);

            ArraySegmentWrapper segmentWrapper = null;
            ResultInfo <User>   result         = null;

            try
            {
                //解析Json字符串
                var    json    = JsonDict.Parse(content);
                string account = json["Account"];
                string pwd     = json["Pwd"];
                //数据库验证
                result = BizManager.AutofacBootstrapper.Resolve <IAuthBiz>().SignIn(account, pwd);

                if (result.Success)
                {
                    result.RData.Password     = string.Empty;
                    session.User              = result.RData;
                    session.User.ConnectState = EConnectState.Online;
                    UserLogin model = new UserLogin()
                    {
                        UserId = result.RData.Id, IP = session.RemoteEndPoint.ToString(), CustomId = session.SessionID, ConnectState = EConnectState.Online
                    };
                    BizManager.AutofacBootstrapper.Resolve <IAuthBiz>().SaveStatus(model);

                    //通知有客户端连入
                    session.NotifyOnline(session.SessionID, account);
                }
            }
            catch (Exception ex)
            {
                result = new ResultInfo <User>(ex);
            }

            //发送登录响应消息
            segmentWrapper = new ArraySegmentWrapper(Constants.SIGNIN_RESPONSE_KEY, JConverter.SerializeToBytes(result));
            session.SendData(session, segmentWrapper.Wrapper());
        }
Exemple #15
0
        private void Send(ArraySegmentWrapper segmentWrapper)
        {
            if (this.IsConnected && !this.m_Client.IsConnected)
            {
                //断线重连
                this.Connect(this.ConnectOptions);
                return;
            }

            try
            {
                ArraySegment <byte> segment = segmentWrapper.Wrapper();
                int count            = segment.Count;
                int maxRequestLength = AppConfiger.AppCfg.CurrCC_Cfg.MaxRequestLength;
                if (count > maxRequestLength)
                {
                    int offset = 0;
                    while (offset < count)
                    {
                        ArraySegment <byte> tmp = new ArraySegment <byte>(segment.Skip(offset).Take(maxRequestLength).ToArray());
                        m_Client.Send(tmp);
                        Thread.Sleep(50);

                        offset += maxRequestLength;
                    }
                }
                else
                {
                    m_Client.Send(segment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #16
0
        public override void ExecuteCommand(ChatSession session, BinaryRequestInfo requestInfo)
        {
            session.Logger.DebugFormat("Cmd: UploadAvatar, RemoteEndPoint: {0}", session.RemoteEndPoint);
            string content = Encoding.UTF8.GetString(requestInfo.Body, 0, requestInfo.Body.Length);
            //session.Logger.DebugFormat("消息内容:{0}", content);

            ResultInfo result = null;

            try
            {
                //解析Json字符串
                var    json    = JsonDict.Parse(content);
                long   userId  = json.GetLong("UserId");
                string extName = json["ExtName"];
                string dataStr = json["Data"];
                byte[] data    = Convert.FromBase64String(dataStr);

                var result2 = BizManager.AutofacBootstrapper.Resolve <IUserBiz>().FindById(userId);
                if (result2.Success)
                {
                    string dir  = Path.Combine("Users", "Avatar", result2.RData.Name);
                    string path = Path.Combine(dir, $"{DateTime.Now.ToString("yyyyMMdd")}_{Guid.NewGuid()}{extName}");
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                    {
                        fs.Write(data, 0, data.Length);
                    }

                    var result3 = BizManager.AutofacBootstrapper.Resolve <IUserBiz>().UpdateUserAvatar(userId, path);
                    if (result3.Success)
                    {
                        result = new ResultInfo()
                        {
                            RMsg = "用户头像上传成功"
                        };
                    }
                    else
                    {
                        result = new ResultInfo()
                        {
                            RCode = "1002", RMsg = "用户头像上传失败", Success = false
                        };
                    }
                }
                else
                {
                    result = new ResultInfo()
                    {
                        RCode = "1001", RMsg = "用户不存在,上传用户头像失败", Success = false
                    };
                }
            }
            catch (Exception ex)
            {
                result = new ResultInfo(ex);
            }

            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.UPLOAD_AVATAR_RESPONSE_KEY, JConverter.SerializeToBytes(result));

            session.SendData(session, segmentWrapper.Wrapper());
        }