public long RandomLoginNumber() { Random rd = new Random(); using (ChatHotContext db = new ChatHotContext()) { while (true) { int LeftCode = rd.Next(10000, 90000); int RightCode = rd.Next(10000, 90000); long code = Convert.ToInt64(LeftCode.ToString() + RightCode); if (db.Users.Where(u => u.UloginNumber == code).Count() == 0) { return(code); } } } }
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 }; } }
//------------------消息处理----------------- 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); }); } } }