Exemple #1
0
        private void BT_Register_Click(object sender, RoutedEventArgs e)
        {
            User u = new User()
            {
                UheadImage   = StaticResource.DefaultUserHeadImageBasePath,
                UloginNumber = RandomLoginNumber(),
                Uname        = TEXT_Name.Text,
                Upassword    = TEXT_Pwd.Text,
            };

            using (ChatHotContext db = new ChatHotContext())
            {
                db.Users.Add(u);
                int result = db.SaveChanges();
                Content = new TextBox()
                {
                    Text = result > 0 ? "注册成功 登录号为\n" + u.UloginNumber : "注册失败", FontSize = 40
                };
            }
        }
Exemple #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);
                });
            }
        }