Esempio n. 1
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());
        }
Esempio n. 2
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());
        }
Esempio n. 3
0
        public void DownloadAvatar(long userId)
        {
            var json = new JsonDict();

            json.AddLong("UserId", userId);

            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.DOWNLOAD_AVATAR_REQUEST_KEY, JConverter.SerializeToBytes(json));

            this.Send(segmentWrapper);
        }
Esempio n. 4
0
        public void UploadAvatar(long userId, string extName, byte[] data)
        {
            var json = new JsonDict();

            json.AddLong("UserId", userId);
            json.Add("ExtName", extName);
            json.Add("Data", Convert.ToBase64String(data));

            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.UPLOAD_AVATAR_REQUEST_KEY, JConverter.SerializeToBytes(json));

            this.Send(segmentWrapper);
        }
Esempio n. 5
0
        public void EditProfile(User user)
        {
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.EDIT_PROFILE_REQUEST_KEY, JConverter.SerializeToBytes(user));

            this.Send(segmentWrapper);
        }
Esempio n. 6
0
        public void SignUp(SignUpReq req)
        {
            ArraySegmentWrapper segmentWrapper = new ArraySegmentWrapper(Constants.SIGNUP_REQUEST_KEY, JConverter.SerializeToBytes(req));

            this.Send(segmentWrapper);
        }
Esempio n. 7
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());
        }
Esempio n. 8
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;
            }
        }
Esempio n. 9
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());
        }
Esempio n. 10
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());
        }
Esempio n. 11
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());
        }
Esempio n. 12
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());
        }
Esempio n. 13
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());
        }
Esempio n. 14
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());
        }
Esempio n. 15
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());
        }
Esempio n. 16
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());
        }