public void UpdateMyFriend(MyFriend myfriend)
 {
     using (SQLiteConnection con = fac.GetConnection(path))
     {
         SQLiteCommand cmd = fac.GetCommand("update  friends_table set NickName=@nickname", new { nickname = myfriend.NickName }, con);
         con.Open();
         cmd.ExecuteNonQuery(CommandBehavior.CloseConnection);
     }
 }
 public void DeleteFriend(MyFriend friend)
 {
     using (SQLiteConnection con = fac.GetConnection(path))
     {
         con.Open();
         SQLiteCommand cmd = fac.GetCommand("delete from friends_table where UserID=@userid", new { userid = friend.UserID }, con);
         cmd.ExecuteNonQuery();
         con.Close();
     }
 }
 public void AddFriend(MyFriend friend)
 {
     using (SQLiteConnection con = fac.GetConnection(path))
     {
         con.Open();
         SQLiteCommand cmd = fac.GetCommand("select count(*) from friends_table where UserID=@userid", new { userid = friend.UserID }, con);
         int count = (int)cmd.ExecuteNonQuery();
         if (count > 0) return;
         cmd = fac.GetCommand("insert into   friends_table(ID,NickName,UserID,UserName) values(@id,@nickname,@userid,@username) ", new { id = friend.ID, nickname = friend.NickName, userid = friend.UserID, username = friend.UserName }, con);
         cmd.ExecuteNonQuery(CommandBehavior.CloseConnection);
         con.Close();
     }
 }
 public IList<MyFriend> GetAllMyFriend(string uid)
 {
     List<MyFriend> lis = new List<MyFriend>();
     using (SQLiteConnection con = fac.GetConnection(path))
     {
         SQLiteCommand cmd = fac.GetCommand("select * from friends_table where UserID=@uid", new { uid = uid }, con);
         con.Open();
         var dr = cmd.ExecuteReader();
         while (dr.Read())
         {
             var user = new MyFriend();
             user.ID = new Guid(dr["ID"].ToString());
             user.UserName = dr["UserName"].ToString();
             user.NickName = dr["NickName"].ToString();
             user.UserID = new Guid(uid);
             lis.Add(user);
         }
         dr.Close();
     }
     return lis;
 }
 public void Remove(MyFriend mf)
 {
     Friends.Remove(mf);
 }
 public void Add(MyFriend mf)
 {
     Friends.Add(mf);
 }
Example #7
0
        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;
        }