Esempio n. 1
0
 public OnlineUser(ServerUser serverUser, NetworkStream stream)
 {
     this.UserName = serverUser.UserName;
     this.NickName = serverUser.NickName;
     this.Gender = serverUser.Gender;
     this.Head = serverUser.Head;
     this.Password = serverUser.Password;
     this.Age = serverUser.Age;
     this.Email = serverUser.Email;
     this.FriendNames = serverUser.FriendNames;
     this.StreamToRemote = stream;
 }
Esempio n. 2
0
 public OnlineUser(ServerUser serverUser, NetworkStream stream)
 {
     this.UserName       = serverUser.UserName;
     this.NickName       = serverUser.NickName;
     this.Gender         = serverUser.Gender;
     this.Head           = serverUser.Head;
     this.Password       = serverUser.Password;
     this.Age            = serverUser.Age;
     this.Email          = serverUser.Email;
     this.FriendNames    = serverUser.FriendNames;
     this.StreamToRemote = stream;
 }
Esempio n. 3
0
        private void AddUserToDatabase(UserDatabase database, string content)
        {
            //元数据content = username|password|nickname|gender|age|email|
            //可能的错误:信息中带有“|”
            string[]   tokens  = content.Split(new char[] { '|' });
            ServerUser newUser = new ServerUser()
            {
                UserName = tokens[0],
                Password = tokens[1],
                NickName = tokens[2],
                Gender   = (GenderOption)Enum.Parse(typeof(GenderOption), tokens[3]),
                Age      = int.Parse(tokens[4]),
                Email    = tokens[5]
            };

            database.AddUser(newUser);
        }
Esempio n. 4
0
 public object Clone()
 {
     ServerUser newServerUser = new ServerUser();
     newServerUser.UserName = this.UserName;
     newServerUser.NickName = this.NickName;
     newServerUser.Gender = this.Gender;
     newServerUser.Age = this.Age;
     newServerUser.Email = this.Email;
     newServerUser.Head = this.Head;
     newServerUser.Password = this.Password;
     newServerUser.FriendNames = new List<string>();
     foreach (string name in this.FriendNames)
     {
         newServerUser.FriendNames.Add(name);
     }
     return newServerUser;
 }
Esempio n. 5
0
        private bool CheckIdentity(string userName, string password)
        {
            if (database.ContainsUser(userName) == false)
            {
                return(false);
            }
            ServerUser currentUser = database.GetUser(userName);

            if (currentUser.Password == password)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 6
0
        public object Clone()
        {
            ServerUser newServerUser = new ServerUser();

            newServerUser.UserName    = this.UserName;
            newServerUser.NickName    = this.NickName;
            newServerUser.Gender      = this.Gender;
            newServerUser.Age         = this.Age;
            newServerUser.Email       = this.Email;
            newServerUser.Head        = this.Head;
            newServerUser.Password    = this.Password;
            newServerUser.FriendNames = new List <string>();
            foreach (string name in this.FriendNames)
            {
                newServerUser.FriendNames.Add(name);
            }
            return(newServerUser);
        }
Esempio n. 7
0
        //获取用户信息
        public ServerUser GetUser(string userName)
        {
            var selectedUser = (from u in dataContext.DatabaseUser
                                where u.UserName == userName
                                select u).Single();
            ServerUser newServerUser = new ServerUser()
            {
                UserName    = selectedUser.UserName,
                NickName    = selectedUser.NickName,
                Age         = selectedUser.Age,
                Email       = selectedUser.Email,
                Gender      = (GenderOption)Enum.Parse(typeof(GenderOption), selectedUser.Gender),
                Password    = selectedUser.Password,
                FriendNames = new List <string>()
            };

            foreach (UserFriend userFriend in selectedUser.UserFriend)
            {
                newServerUser.FriendNames.Add(userFriend.UserName);
            }
            return(newServerUser);
        }
Esempio n. 8
0
        //添加用户
        public void AddUser(ServerUser user)
        {
            DatabaseUser newUser = new DatabaseUser()
            {
                UserName = user.UserName,
                NickName = user.NickName,
                Gender   = user.Gender.ToString(),
                Email    = user.Email,
                Age      = user.Age,
                Password = user.Password
            };

            foreach (string username in user.FriendNames)
            {
                UserFriend newFriend = new UserFriend()
                {
                    UserName = username
                };
                newUser.UserFriend.Add(newFriend);
            }
            dataContext.DatabaseUser.InsertOnSubmit(newUser);
            dataContext.SubmitChanges();
        }
Esempio n. 9
0
 //添加用户
 public void AddUser(ServerUser user)
 {
     DatabaseUser newUser = new DatabaseUser()
     {
         UserName = user.UserName,
         NickName = user.NickName,
         Gender = user.Gender.ToString(),
         Email = user.Email,
         Age = user.Age,
         Password = user.Password
     };
     foreach (string username in user.FriendNames)
     {
         UserFriend newFriend = new UserFriend() { UserName = username };
         newUser.UserFriend.Add(newFriend);
     }
     dataContext.DatabaseUser.InsertOnSubmit(newUser);
     dataContext.SubmitChanges();
 }
Esempio n. 10
0
 //更新用户信息
 public void UpdateUser(string userName, ServerUser newUser)
 {
     this.RemoveUser(userName);
     this.AddUser(newUser);
 }
Esempio n. 11
0
 //查询用户
 public List<ServerUser> SearchUser(string userName, string nickName, string gender, int age)
 {
     List<ServerUser> usersFound = new List<ServerUser>();
     if (this.ContainsUser(userName))
     {
         usersFound.Add(this.GetUser(userName));
     }
     //寻找昵称精确匹配
     var nickNameMatches = from u in dataContext.DatabaseUser
                           where u.NickName == nickName
                           select u;
     foreach (DatabaseUser user in nickNameMatches)
     {
         ServerUser newServerUser = new ServerUser()
         {
             UserName = user.UserName,
             NickName = user.NickName,
             Age = user.Age,
             Email = user.Email,
             Gender = (GenderOption)Enum.Parse(typeof(GenderOption), user.Gender),
             Password = user.Password,
         };
         usersFound.Add(newServerUser);
     }
     //寻找昵称粗匹配
     var nickNameRoughMatches = from u in dataContext.DatabaseUser
                                where u.NickName.Contains(nickName) && u.NickName != nickName
                                select u;
     foreach (DatabaseUser user in nickNameRoughMatches)
     {
         ServerUser newServerUser = new ServerUser()
         {
             UserName = user.UserName,
             NickName = user.NickName,
             Age = user.Age,
             Email = user.Email,
             Gender = (GenderOption)Enum.Parse(typeof(GenderOption), user.Gender),
             Password = user.Password,
         };
         usersFound.Add(newServerUser);
     }
     //按条件过滤
     if (gender.ToLower() != "unlimited")
     {
         List<ServerUser> usersToDelete = new List<ServerUser>();
         GenderOption filter =
             (GenderOption)Enum.Parse(typeof(GenderOption), gender);
         foreach (ServerUser user in usersFound)
         {
             if (user.Gender != filter)
             {
                 usersToDelete.Add(user);
             }
         }
         foreach (ServerUser userToDelete in usersToDelete)
         {
             usersFound.Remove(userToDelete);
         }
     }
     if (age != -1)
     {
         List<ServerUser> usersToDelete = new List<ServerUser>();
         foreach (ServerUser user in usersFound)
         {
             if (user.Age != age)
             {
                 usersToDelete.Add(user);
             }
         }
         foreach (ServerUser userToDelete in usersToDelete)
         {
             usersFound.Remove(userToDelete);
         }
     }
     return usersFound;
 }
Esempio n. 12
0
 //获取用户信息
 public ServerUser GetUser(string userName)
 {
     var selectedUser = (from u in dataContext.DatabaseUser
                         where u.UserName == userName
                         select u).Single();
     ServerUser newServerUser = new ServerUser()
     {
         UserName = selectedUser.UserName,
         NickName = selectedUser.NickName,
         Age = selectedUser.Age,
         Email = selectedUser.Email,
         Gender = (GenderOption)Enum.Parse(typeof(GenderOption), selectedUser.Gender),
         Password = selectedUser.Password,
         FriendNames = new List<string>()
     };
     foreach (UserFriend userFriend in selectedUser.UserFriend)
     {
         newServerUser.FriendNames.Add(userFriend.UserName);
     }
     return newServerUser;
 }
Esempio n. 13
0
 private void AddUserToDatabase(UserDatabase database, string content)
 {
     //元数据content = username|password|nickname|gender|age|email|
     //可能的错误:信息中带有“|”
     string[] tokens = content.Split(new char[] { '|' });
     ServerUser newUser = new ServerUser()
     {
         UserName = tokens[0],
         Password = tokens[1],
         NickName = tokens[2],
         Gender = (GenderOption)Enum.Parse(typeof(GenderOption), tokens[3]),
         Age = int.Parse(tokens[4]),
         Email = tokens[5]
     };
     database.AddUser(newUser);
 }
Esempio n. 14
0
        private void ProcessMessage(object sender, MessageReceivedEventArgs e)
        {
            string originalString = e.Content;
            //从string获取Message
            Message msg = GenerateMessage(originalString);

            //注册
            if (msg.Type == MessageType.REGISTER)
            {
                //数据库中有此用户,注册失败
                if (database.ContainsUser(msg.Sender))
                {
                    RespondInvalid(e);
                }
                //数据库中无此用户,注册成功
                else
                {
                    RespondOK(e);
                    AddUserToDatabase(database, msg.Content);
                }
            }
            //登录
            else if (msg.Type == MessageType.LOGIN)
            {
                string userName = msg.Sender;
                string password = msg.Content;
                if (CheckIdentity(userName, password) && !onlineUsers.Contains(userName))
                {
                    //回应ok
                    RespondOK(e);
                    //将用户加入在线用户表
                    ServerUser fetchedServerUser = database.GetUser(userName);
                    OnlineUser newOnlineUser     = new OnlineUser(fetchedServerUser, e.StreamToRemote);
                    onlineUsers.Add(userName, newOnlineUser);
                    //发送用户好友列表
                    SendFriendList(e, userName);
                    Thread.Sleep(100);
                    //发送用户群聊列表
                    SendGroupChatList(e, userName);
                    //发送用户在线好友列表,发送给在线好友JOIN信息
                    //SendOnlineFriendList(e, userName);
                    Thread.Sleep(200);
                    //发送用户离线时收到的信息
                    SendOfflineMessage(e, userName);
                    //触发在线用户改变事件
                    if (OnlineUserChanged != null)
                    {
                        OnlineUserChanged(this, new OnlineUserChangedEventArgs(newOnlineUser.UserName, "online"));
                    }
                }
                else
                {
                    RespondInvalid(e);
                }
            }
            //查询好友
            else if (msg.Type == MessageType.FRIENDSEARCH)
            {
                string[] keyWords = msg.Content.Split(new char[] { '|' });
                string   userName = keyWords[0];
                string   nickName = keyWords[1];
                string   gender   = keyWords[2]; //注意此处gender为string以方便查询
                int      age      = -1;
                if (keyWords[3].Trim().ToLower() != "unlimited")
                {
                    age = int.Parse(keyWords[3].Trim());
                }
                List <ServerUser> usersFound = database.SearchUser(userName, nickName, gender, age);
                if (usersFound.Count == 0)
                {
                    Message newMessage = new Message(MessageType.FRIENDSEARCH, "SERVER", "empty");
                    FowardTo(e, msg.Sender, newMessage);
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (ServerUser userFound in usersFound)
                    {
                        string newUserName = userFound.UserName;
                        string newNickName = userFound.NickName;
                        string newGender   = userFound.Gender.ToString();
                        string newAge      = userFound.Age.ToString();
                        string newEmail    = userFound.Email;
                        sb.AppendFormat("{0}|{1}|{2}|{3}|{4}|", newUserName, newNickName,
                                        newGender, newAge, newEmail);
                    }
                    string  newContent = sb.ToString();
                    Message newMessage = new Message(MessageType.FRIENDSEARCH, "SERVER", newContent);
                    FowardTo(e, msg.Sender, newMessage);
                }
            }
            //私聊,转发给目标用户,写入总聊天记录
            else if (msg.Type == MessageType.PRIV)
            {
                bool IsSuccess = FowardTo(e, msg.Receiver, msg);
                if (IsSuccess)
                {
                    database.AddMessage(msg);
                    RespondOK(e);
                }
                else
                {
                    RespondInvalid(e);
                }
            }
            //群聊,获取群信息,写入总聊天记录,转发给目标用户
            else if (msg.Type == MessageType.GROUP)
            {
                int        chatNumber = int.Parse(msg.Receiver);
                ServerChat targetChat = database.GetChat(chatNumber);
                foreach (string userName in targetChat.ChatUserNames)
                {
                    if (userName != msg.Sender)
                    {
                        try
                        {
                            FowardTo(e, userName, msg);
                        }
                        catch
                        {
                            RespondInvalid(e);
                        }
                    }
                }
                database.AddMessage(msg);
                RespondOK(e);
            }
            //用户离线
            else if (msg.Type == MessageType.LEAVE)
            {
                string     userName    = msg.Sender;
                OnlineUser currentUser = onlineUsers[userName] as OnlineUser;
                //向在线好友发送离线消息
                foreach (string friendName in currentUser.FriendNames)
                {
                    if (onlineUsers.Contains(friendName))
                    {
                        FowardTo(e, friendName, msg);
                    }
                }
                //触发在线用户改变事件
                if (OnlineUserChanged != null)
                {
                    OnlineUserChanged(this,
                                      new OnlineUserChangedEventArgs(onlineUsers[userName] as OnlineUser, "offline"));
                }
                //是否要在此关闭用户连接?
                //将用户移出在线用户列表
                onlineUsers.Remove(userName);
            }
            //好友申请消息
            else if (msg.Type == MessageType.FRIENDREQUEST)
            {
                MakeFriend(msg.Sender, msg.Receiver);
                bool IsSuccess = FowardTo(e, msg.Receiver, msg);
                if (IsSuccess)
                {
                    database.AddMessage(msg);
                    RespondOK(e);
                }
                else
                {
                    RespondInvalid(e);
                }
            }
            //创建群聊
            else if (msg.Type == MessageType.CREATECHAT)
            {
                ServerChat newChat = new ServerChat(msg.Receiver);
                string[]   tokens  = msg.Content.Split(new char[] { '|' });
                foreach (string token in tokens)
                {
                    if (!string.IsNullOrEmpty(token))
                    {
                        newChat.AddUserName(token.Trim());
                    }
                }
                //在数据库中创建群聊
                try
                {
                    database.AddChat(newChat);
                }
                catch
                {
                    RespondInvalid(e);
                }
                //将信息转发给群聊参与者
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}|", newChat.ChatNumber.ToString());
                sb.AppendFormat("{0}|", newChat.ChatName);
                foreach (var user in newChat.ChatUserNames)
                {
                    sb.AppendFormat("{0}|", user);
                }
                Message msgToClient = new Message(MessageType.CREATECHAT, msg.SendTime,
                                                  msg.Sender, sb.ToString());
                bool IsSuccess = true;
                foreach (string userName in newChat.ChatUserNames)
                {
                    IsSuccess = FowardTo(e, userName, msgToClient);
                }
                //回应
                if (IsSuccess)
                {
                    RespondOK(e);
                }
                else
                {
                    RespondOK(e);
                }
            }
            //离开群聊
            else if (msg.Type == MessageType.LEAVECHAT)
            {
                int    chatNumber = int.Parse(msg.Content);
                string userName   = msg.Sender;
                //在数据库中更新群聊信息
                try
                {
                    database.DeleteChatUser(chatNumber, userName);
                }
                catch
                {
                    RespondInvalid(e);
                }
                //向群聊者推送离开信息
                ServerChat currentChat = database.GetChat(chatNumber);
                bool       IsSuccess   = true;
                foreach (string name in currentChat.ChatUserNames)
                {
                    if (name != userName)
                    {
                        IsSuccess = FowardTo(e, name, msg);
                    }
                }
                if (IsSuccess)
                {
                    RespondOK(e);
                }
                else
                {
                    RespondOK(e);
                }
            }
        }
Esempio n. 15
0
 //更新用户信息
 public void UpdateUser(string userName, ServerUser newUser)
 {
     this.RemoveUser(userName);
     this.AddUser(newUser);
 }
Esempio n. 16
0
        //查询用户
        public List <ServerUser> SearchUser(string userName, string nickName, string gender, int age)
        {
            List <ServerUser> usersFound = new List <ServerUser>();

            if (this.ContainsUser(userName))
            {
                usersFound.Add(this.GetUser(userName));
            }
            //寻找昵称精确匹配
            var nickNameMatches = from u in dataContext.DatabaseUser
                                  where u.NickName == nickName
                                  select u;

            foreach (DatabaseUser user in nickNameMatches)
            {
                ServerUser newServerUser = new ServerUser()
                {
                    UserName = user.UserName,
                    NickName = user.NickName,
                    Age      = user.Age,
                    Email    = user.Email,
                    Gender   = (GenderOption)Enum.Parse(typeof(GenderOption), user.Gender),
                    Password = user.Password,
                };
                usersFound.Add(newServerUser);
            }
            //寻找昵称粗匹配
            var nickNameRoughMatches = from u in dataContext.DatabaseUser
                                       where u.NickName.Contains(nickName) && u.NickName != nickName
                                       select u;

            foreach (DatabaseUser user in nickNameRoughMatches)
            {
                ServerUser newServerUser = new ServerUser()
                {
                    UserName = user.UserName,
                    NickName = user.NickName,
                    Age      = user.Age,
                    Email    = user.Email,
                    Gender   = (GenderOption)Enum.Parse(typeof(GenderOption), user.Gender),
                    Password = user.Password,
                };
                usersFound.Add(newServerUser);
            }
            //按条件过滤
            if (gender.ToLower() != "unlimited")
            {
                List <ServerUser> usersToDelete = new List <ServerUser>();
                GenderOption      filter        =
                    (GenderOption)Enum.Parse(typeof(GenderOption), gender);
                foreach (ServerUser user in usersFound)
                {
                    if (user.Gender != filter)
                    {
                        usersToDelete.Add(user);
                    }
                }
                foreach (ServerUser userToDelete in usersToDelete)
                {
                    usersFound.Remove(userToDelete);
                }
            }
            if (age != -1)
            {
                List <ServerUser> usersToDelete = new List <ServerUser>();
                foreach (ServerUser user in usersFound)
                {
                    if (user.Age != age)
                    {
                        usersToDelete.Add(user);
                    }
                }
                foreach (ServerUser userToDelete in usersToDelete)
                {
                    usersFound.Remove(userToDelete);
                }
            }
            return(usersFound);
        }