private void load_all_friends() { FriendList = new List<MyFriend>(); FriendDAL FriendDAL = new Chatting.DAL.FriendDAL(); FriendDAL.Path = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "clientdb.db"); var fs = FriendDAL.GetAllMyFriend(App.CurrentUser.ID.ToString()); if (fs != null && fs.Count > 0) { FriendList.AddRange(fs); } }
public App() : base() { CliendSocket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); CliendSocket.Blocking = true; FriendDAL FriendDAL = new Chatting.DAL.FriendDAL(); OnLogin += new LoginBack(App_OnLogin); listenThread = new Thread(new ThreadStart(() => { while (true) { Thread.Sleep(1000); var response = CliendSocket.SocketGet<Response>(); if (response == null) continue; switch (response.ProcessType) { case ProcessType.Chatting: { #region 聊天 Request req = response.Data as Request; MyFriend my = GetFriend(req.SourceUserName); this.Dispatcher.Invoke(new Action(() => { ChatWin.Add(req.SourceUserName, my.NickName, req.Data.ToString()); ChatWin.Show(); }), null); break; #endregion } case ProcessType.AddNewFriend: #region 添加新朋友 if (!response.Success) { MessageBox.Show(response.Data.ToString()); break; } var request = response.Data as Request; if (request.Data is NewFriendRequest) { var res = (response.Data as Request); NewFriendRequest nfr = request.Data as NewFriendRequest; this.Dispatcher.Invoke(new Action(() => { var nf = new NewFriendMessage(); nf.UserName = res.SourceUserName; nf.Message = (res.Data as NewFriendRequest).Message; nf.Show(); })); } else if (request.Data is NewFriendResponse) { NewFriendResponse nfr = request.Data as NewFriendResponse; MessageBox.Show(request.SourceUserName + " accept " + nfr.Accept); if (nfr.Accept) { Request re2 = new Request(); re2.SourceUserName = CurrentUser.UserName; re2.TargetUserName = request.SourceUserName; re2.ProcessType = ProcessType.UpdateFriendInfo; CliendSocket.SocketSend(re2); } } else { } #endregion break; case ProcessType.DeleteFriend: { Dispatcher.Invoke(new Action(() => { var req = response.Data as Request; if (MessageBox.Show(req.SourceUserName + "已经和你解除了好友关系,点击确定,删除她/他,点击取消保留", "通知", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { DeleteFriend(req.SourceUserName); } else { } }), null); break; } case ProcessType.Login: if (OnLogin != null) { OnLogin(response); } break; case ProcessType.Logout: break; case ProcessType.UpdateUserInfo: break; case ProcessType.UpdateFriendInfo: #region 更新朋友信息 if (response.Success) { var f = response.Data.ToString().Split(';'); MyFriend mf = new MyFriend(); mf.UserID = new Guid(CurrentUser.ID.ToString()); mf.UserName = f[0]; mf.NickName = f[1]; if (FriendList.All(p => p.UserName != f[0])) { FriendDAL.AddFriend(mf); FriendList.Add(mf); this.Dispatcher.Invoke(new Action(() => { FriendMainForm.Add(mf); FriendMainForm.Show(); if (GroupChatWin != null) { GroupChatWin.Add(mf.UserName, mf.NickName); } })); } } break; #endregion case ProcessType.Register: if (OnRegister != null) { OnRegister(response); } break; case ProcessType.File: #region 传送文件 if (MessageBox.Show("收到文件,是否查看?", "消息", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { SaveFileDialog sfd = new SaveFileDialog(); var ff = response.Data as Chatting.Protocol.FileInfo; sfd.FileName = Path.GetFileName(ff.FileName); if (sfd.ShowDialog().Value) { var s = File.Create(sfd.FileName); s.Write(ff.File, 0, ff.File.Length); s.Flush(); s.Close(); s.Dispose(); } } break; #endregion case ProcessType.GroupChatting: var u = response.Data as Request; string uname = u.SourceUserName; string content = u.Data.ToString(); Dispatcher.Invoke(new Action(() => { GroupChatWin.AddChat(uname, content); GroupChatWin.Show(); }), null); break; default: break; } } })); listenThread.IsBackground = true; }
public void DeleteFriend(string name) { FriendDAL fd = new Chatting.DAL.FriendDAL(); var myfr = FriendList.Where(p => p.UserName == name).Single(); fd.DeleteFriend(myfr); FriendList.Remove(myfr); if (FriendMainForm != null) FriendMainForm.Friends.Remove(myfr); if (ChatWin != null) ChatWin.Remove(name); if (GroupChatWin != null) { GroupChatWin.Remove(name); } }