示例#1
0
        //------------------消息处理-----------------
        private void ClientSocket_GetNewMessage_ChatForm(MessageModel Msg, System.Net.EndPoint From)
        {
            if (Msg.MessageType == MessageType.String)
            {
                MessageContent_String c = MessageModel.ToModel <MessageContent_String>(Msg.MessageContent);
                Msg.MessageContent = c;
                PrintMessageRouter();
            }
            else if (Msg.MessageType == MessageType.Get)
            {
                MessageContent_Get get = MessageModel.ToModel <MessageContent_Get>(Msg.MessageContent);
                if (get.RequestType == Get_Type.File)
                {
                    if (get.RequestFileType == File_Type.HeadImagoe)
                    {
                        IoTool.CreateDirectory(StaticResource.UserHeadImageBasePath);
                        String headPath = StaticResource.ToUserHeadImagePath(get.RequestArg + ".png");
                        IoTool.SaveFile(headPath, get.Content);
                        Dispatcher.Invoke(() =>
                        {
                            StaticResource.SetHeadImageSource(long.Parse(get.RequestArg.ToString()), ImageTool.GetUserHeadeImage(long.Parse(get.RequestArg.ToString())));
                        });
                    }
                }
            }
            else if (Msg.MessageType == MessageType.Login)
            {
                MessageContent_Login login = MessageModel.ToModel <MessageContent_Login>(Msg.MessageContent);
                if (login.LoginType == LoginType.OffOnLine)
                {
                    MessageBox.Show(MessageStringMark.GetCodeMsg(login.WithMessage));
                    Dispatcher.Invoke(() =>
                    {
                        OnCloseALL();
                    });
                }
            }
            else if (Msg.MessageType == MessageType.DataPacket)
            {
                MessageContent_DataPacket packet = MessageModel.ToModel <MessageContent_DataPacket>(Msg.MessageContent);
                //如果是图片的数据
                if (packet.PacketType == DataPacketType.ImageDataPacket)
                {
                    String fname = packet.FileName;
                    //如果是第一个包
                    if (packet.PacketIndex == 1)
                    {
                        DataPacketsFile.Add(fname, new WritingFile(StaticResource.MessageImagePath + fname)
                        {
                            Tag = packet.FileToken//这条实在没什么用
                        });
                    }
                    DataPacketsFile[fname].Writer(packet.Content);
                    if (packet.IsLastPacket)
                    {
                        DataPacketsFile[fname].WriterEnd();
                    }
                }
            }
            else if (Msg.MessageType == MessageType.Image)
            {
                MessageContent_Image img = MessageModel.ToModel <MessageContent_Image>(Msg.MessageContent);
                Msg.MessageContent = img;
                PrintMessageRouter();
            }
            else if (Msg.MessageType == MessageType.MakeFriend)
            {
                ChatHotContext DB = new ChatHotContext();

                MessageContent_MakeFriend content = MessageModel.ToModel <MessageContent_MakeFriend>(Msg.MessageContent);
                Msg.MessageContent = content;
                if (content.MakeFriendType == MakeFriendType.SendStart)
                {
                    if (MessageBox.Show("好友请求", "来自" + content, MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
                    {
                        //这个是返回消息所以是反着来的
                        //SocketTool.GetClientUDP().SendToServer(TheMessageModel.MAKEFRIEND(content.ToNum, content.FromNum, content.FromNum, content.ToNum, MakeFriendType.Success));
                        Msg.FromIpaddress      = StaticResource.IPV4Address.ToString();
                        Msg.FromLoginNumber    = content.ToNum;
                        Msg.ToLoginNumber      = content.FromNum;
                        content.MakeFriendType = MakeFriendType.Success;
                        Msg.MessageContent     = content;
                        SocketTool.GetClientUDP().SendToServer(Msg);
                        Dispatcher.Invoke(() =>
                        {
                            UserListForm.MainUserListForm.AddFriendsItem(DB.Users.Where(u => u.UloginNumber == content.FromNum).First());
                        });
                    }
                }
                else if (content.MakeFriendType == MakeFriendType.Success)
                {
                    MessageBox.Show("对方同意");
                    Dispatcher.Invoke(() =>
                    {
                        UserListForm.MainUserListForm.AddFriendsItem(DB.Users.Where(u => u.UloginNumber == content.ToNum).First());
                    });
                }
                else if (content.MakeFriendType == MakeFriendType.Fail)
                {
                    MessageBox.Show("对方拒绝");
                }
                else if (content.MakeFriendType == MakeFriendType.DeleteSuccess)
                {
                    Dispatcher.Invoke(() => {
                        UserListForm.MainUserListForm.RemoveFriendsItem(UserSource.UloginNumber == content.FromNum ? content.ToNum : content.FromNum);
                    });
                }
                DB.Dispose();
                return;
            }
            else if (Msg.MessageType == MessageType.UpdateUser)
            {
                MessageContent_UpdateUser content = MessageModel.ToModel <MessageContent_UpdateUser>(Msg.MessageContent);
                StaticResource.ALLUserSource = content.GetContent();
                Dispatcher.Invoke(() =>
                {
                    UserInfoChanged(content.GetContent());
                });
                //PrintMessageRouter(content);
            }
            //如果是需要提醒用户的消息就调用一下这个方法
            void PrintMessageRouter(Object content = null)
            {
                if (content != null)
                {
                    Msg.MessageContent = content;
                }
                //如果存在打开的对应登录号的聊天窗体
                if (MainChatForm != null && HasChatUser(Msg.FromLoginNumber))
                {
                    Dispatcher.Invoke(() =>
                    {
                        User UserFrom = GetUserByChatUsersItem(Msg.FromLoginNumber);
                        PrintMessage(Msg, UserFrom, UserSource);
                    });
                }
                //如果不存在
                else
                {
                    //MessageBox.Show("有新的消息--->>" + Msg.FromLoginNumber);
                    Dispatcher.Invoke(() =>
                    {
                        UserListForm.MainUserListForm.AppendToMessageQueue(Msg);
                    });
                }
            }
        }
示例#2
0
        private void SocketServer_GetNewMessage(MessageModel Msg, EndPoint From)
        {
            Object b = Msg.MessageContent;

            if (Msg.MessageType == MessageType.Login)
            {
                MessageContent_Login info = MessageModel.ToModel <MessageContent_Login>(Msg.MessageContent);
                //如果是登录消息
                if (info.LoginType == LoginType.Submit)
                {
                    //这里判断是否登录成功,返回一个Result类型的Login信息
                    if (IsOnline(info.GetContent().UloginNumber ?? 0))
                    {
                        SocketServer.SendTo(new MessageModel(MessageType.Login, 0, info.GetContent().UloginNumber ?? 0, SocketTool.IPV4Address.ToString(), new MessageContent_Login(LoginType.Result, info.GetContent(), MessageStringMark.GetCodeMsg(MessageStringMark.CodeMessageType.LoginError_UserOnline).ToString())), From);
                        return;
                    }
                    Dispatcher.Invoke(() =>
                    {
                        AddOnlineUser(new UserInfo()
                        {
                            UserIP = From,
                            User   = info.GetContent(),
                        });
                        SocketServer.SendTo(new MessageModel(MessageType.Login, 0, info.GetContent().UloginNumber ?? 0, SocketTool.IPV4Address.ToString(), new MessageContent_Login(LoginType.Result, info.GetContent(), MessageStringMark.GetCodeMsg(MessageStringMark.CodeMessageType.LoginSuccess).ToString())), From);
                    });
                }
                //如果是在线确认消息
                else if (info.LoginType == LoginType.ImOnline)
                {
                    long clientNum = info.GetContent().UloginNumber ?? 0;
                    //如果已存在
                    if (OnlineUsersDateTime.Keys.Where(k => k == clientNum).Count() > 0)
                    {
                        OnlineUsersDateTime[clientNum] = DateTime.Now;
                    }
                    else
                    {
                        //如果不存在就表明已经被下线了
                        // OnlineUsersDateTime.Add(clientNum, DateTime.Now);
                    }
                }
            }
            else
            {
                //!!!!!!!!!!!!!!!!
                //!!!!!!!!!!!!!!!!
                //这里的消息需要手动添加
                //不然无法显示出来
                MessageContent_Base content_base = MessageModel.ToModel <MessageContent_Base>(Msg.MessageContent);
                //如果是文字消息
                if (Msg.MessageType == MessageType.String)
                {
                    MessageContent_String info = MessageModel.ToModel <MessageContent_String>(Msg.MessageContent);
                    content_base = info;
                    MessageRouter();
                }
                else if (Msg.MessageType == MessageType.Get)
                {
                    MessageContent_Get m = MessageModel.ToModel <MessageContent_Get>(Msg.MessageContent);
                    content_base = m;
                    long lnum = long.Parse(m.RequestArg.ToString());
                    if (m.RequestType == Get_Type.File)
                    {
                        if (m.RequestFileType == File_Type.HeadImagoe)
                        {
                            User u = DB.Users.Where(u => u.UloginNumber == lnum).First();
                            m.RequestArg = u.UheadImage;
                        }
                        if (IoTool.IsExists(m.RequestArg.ToString()))
                        {
                            m.Content = IoTool.GetFileByte(m.RequestArg.ToString());
                        }
                        else
                        {
                            m.Content = IoTool.GetFileByte(StaticResource.DefaultUserHeadImageBasePath);
                        }
                        m.RequestArg = lnum.ToString();
                    }
                    SocketServer.SendTo(new MessageModel(MessageType.Get, 0, Msg.FromLoginNumber, StaticResource.IPV4Address.ToString(),
                                                         m), From);
                }
                else if (Msg.MessageType == MessageType.DataPacket)
                {
                    MessageContent_DataPacket data = MessageModel.ToModel <MessageContent_DataPacket>(Msg.MessageContent);
                    content_base = data;
                    MessageRouter();
                }
                else if (Msg.MessageType == MessageType.Image)
                {
                    MessageContent_Image img = MessageModel.ToModel <MessageContent_Image>(Msg.MessageContent);
                    content_base = img;
                    MessageRouter();
                }
                //如果是加好友
                else if (Msg.MessageType == MessageType.MakeFriend)
                {
                    MessageContent_MakeFriend friend = MessageModel.ToModel <MessageContent_MakeFriend>(Msg.MessageContent);
                    content_base = friend;
                    if (friend.MakeFriendType == MakeFriendType.SendStart)
                    {
                        MessageRouter();
                    }
                    else if (friend.MakeFriendType == MakeFriendType.Success)
                    {
                        DB.Friends.Add(new Friend()
                        {
                            AcceptUser = friend.ToNum,
                            SendUser   = friend.FromNum,
                            CreateDate = friend.CreateDate,
                            LockDate   = DateTime.UtcNow,
                        });
                        if (DB.SaveChanges() > 0)
                        {
                            MessageRouter();
                        }
                    }
                    //如果是删好友
                    else if (friend.MakeFriendType == MakeFriendType.Delete)
                    {
                        DB.Entry(DB.Friends.Where(f => f.SendUser == friend.FromNum && f.AcceptUser == friend.ToNum || f.SendUser == friend.ToNum & f.AcceptUser == friend.FromNum).First()).State = EntityState.Deleted;
                        //如果数据库删除成功
                        if (DB.SaveChanges() > 0)
                        {
                            //把消息给两方发过去
                            friend.MakeFriendType = MakeFriendType.DeleteSuccess;
                            Msg.MessageContent    = friend;
                            MessageRouter();
                            MessageRouterToFrom();
                        }
                    }
                }
                else if (Msg.MessageType == MessageType.UpdateUser)
                {
                    MessageContent_UpdateUser content = MessageModel.ToModel <MessageContent_UpdateUser>(Msg.MessageContent);
                    Msg.MessageContent = content;
                    User   ContentUser       = content.GetContent();
                    User   EFuser            = DB.Users.Where(u => u.Uid == ContentUser.Uid).First();
                    String HeadImageSavePath = null;
                    //如果上传头像了
                    if (content.HeadImageBytes.Length != 0)
                    {
                        //保存图片 把地址放到数据库
                        HeadImageSavePath = StaticResource.UserHeadImageBasePath + ContentUser.UloginNumber + ".png";
                        IoTool.SaveFile(HeadImageSavePath, content.HeadImageBytes);
                        EFuser.UheadImage = HeadImageSavePath;
                    }

                    EFuser.Uname     = ContentUser.Uname;
                    EFuser.Upassword = ContentUser.Upassword;
                    EFuser.Utag      = ContentUser.Utag;

                    DB.Update(EFuser);
                    if (DB.SaveChanges() > 0)
                    {
                        //成功后再把修改后的User发送回去
                        //如果想要所有人都实时改变就再判断一下分别向好友发送更改后的User
                        content.SetContent(EFuser);
                        Msg.MessageContent = content;
                        MessageRouterToFrom();
                    }
                }


                //将当前消息进行统一处理
                void MessageRouter(MessageContent_Base c = null)
                {
                    if (c != null)
                    {
                        Msg.MessageContent = c;
                    }
                    //如果目标在线
                    if (IsOnline(Msg.ToLoginNumber))
                    {
                        UserInfo UserTo = OnlineUsers[Msg.ToLoginNumber];
                        SocketTool.GetServerUDP().SendTo(Msg, UserTo.UserIP);
                    }
                    //如果目标不在线
                    //输入到数据库------------->
                    else
                    {
                    }
                }

                //将消息返回给发送者
                void MessageRouterToFrom()
                {
                    //如果目标在线
                    if (IsOnline(Msg.FromLoginNumber))
                    {
                        UserInfo UserTo = OnlineUsers[Msg.FromLoginNumber];
                        SocketTool.GetServerUDP().SendTo(Msg, UserTo.UserIP);
                    }
                    //如果目标不在线
                    //输入到数据库------------->
                    else
                    {
                    }
                }

                Dispatcher.Invoke(() =>
                {
                    Msg.MessageContent      = content_base;
                    DisplayMessageModel msg = new DisplayMessageModel()
                    {
                        IsFrom = true,
                        Msg    = Msg,
                    };
                    LIST_MessageLogs.Items.Add(msg);
                    LIST_MessageLogs.ScrollIntoView(msg);
                });
            }
        }