Example #1
0
        //使两个用户成为好友
        private void MakeFriend(string sender, string receiver)
        {
            //更新服务端发送请求用户的信息
            OnlineUser currentUser = onlineUsers[sender] as OnlineUser;

            currentUser.FriendNames.Add(receiver);
            //对于接收方,若在线则立刻更新服务端
            if (onlineUsers.Contains(receiver))
            {
                currentUser = onlineUsers[receiver] as OnlineUser;
                currentUser.FriendNames.Add(sender);
            }
            //更新数据库
            database.MakeFriends(sender, receiver);
            //向接收方发送消息在本方法外完成
        }
Example #2
0
        //发送在线好友列表
        private void SendOnlineFriendList(MessageReceivedEventArgs e, string userName)
        {
            //格式ONLINELIST|time|SERVER|username1|username2|...
            //注意为空的情况
            StringBuilder sb          = new StringBuilder();
            OnlineUser    currentUser = onlineUsers[userName] as OnlineUser;

            if (currentUser.FriendNames.Count != 0)
            {
                foreach (string friendName in currentUser.FriendNames)
                {
                    if (onlineUsers.Contains(friendName))
                    {
                        sb.AppendFormat("{0}|", friendName);
                        //发送上线信息,格式为JOIN|time|username
                        Message msgToFriend = new Message(MessageType.JOIN, userName, string.Empty);
                        FowardTo(e, friendName, msgToFriend);
                    }
                }
                string  content    = sb.ToString();
                Message newMessage = new Message(MessageType.ONLINELIST, "SERVER", content);
                FowardTo(e, userName, newMessage);
            }
        }
Example #3
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);
         }
     }
 }
Example #4
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);
                }
            }
        }