Пример #1
0
 private void AddMessageinCache(MessageDetail _MessageDetail)
 {
     CurrentMessage.Add(_MessageDetail);
     if (CurrentMessage.Count > 100)
     {
         CurrentMessage.RemoveAt(0);
     }
 }
Пример #2
0
        public void SendPrivateMessage(string toUserId, string message)
        {
            try
            {
                string fromconnectionid = Context.ConnectionId;
                string strfromUserId    = (ConnectedUsers.Where(u => u.ConnectionId == Context.ConnectionId).Select(u => u.UserID).FirstOrDefault()).ToString();
                int    _fromUserId      = 0;
                int.TryParse(strfromUserId, out _fromUserId);
                int _toUserId = 0;
                int.TryParse(toUserId, out _toUserId);
                List <UserDetail> FromUsers = ConnectedUsers.Where(u => u.UserID == _fromUserId).ToList();
                List <UserDetail> ToUsers   = ConnectedUsers.Where(x => x.UserID == _toUserId).ToList();

                if (FromUsers.Count != 0 && ToUsers.Count() != 0)
                {
                    foreach (var ToUser in ToUsers)
                    {
                        // send to                                                                                            //Chat Title
                        Clients.Client(ToUser.ConnectionId).sendPrivateMessage(_fromUserId.ToString(), FromUsers[0].UserName, FromUsers[0].UserName, message);
                    }


                    foreach (var FromUser in FromUsers)
                    {
                        // send to caller user                                                                                //Chat Title
                        Clients.Client(FromUser.ConnectionId).sendPrivateMessage(_toUserId.ToString(), FromUsers[0].UserName, ToUsers[0].UserName, message);
                    }
                    // send to caller user
                    //Clients.Caller.sendPrivateMessage(_toUserId.ToString(), FromUsers[0].UserName, message);
                    //ChatDB.Instance.SaveChatHistory(_fromUserId, _toUserId, message);
                    MessageDetail _MessageDeail = new MessageDetail {
                        FromUserID = _fromUserId, FromUserName = FromUsers[0].UserName, ToUserID = _toUserId, ToUserName = ToUsers[0].UserName, Message = message
                    };
                    AddMessageinCache(_MessageDeail);
                    //save message in db

                    using (ChatAppEntities1 dc = new ChatAppEntities1())
                    {
                        var messageDetail = new MessageHistory
                        {
                            Message = _MessageDeail.FromUserID + '-' + _MessageDeail.FromUserName + '-' + _MessageDeail.ToUserID + '-' + _MessageDeail.ToUserName + '-' + _MessageDeail.Message,
                        };
                        dc.MessageHistories.Add(messageDetail);
                        dc.SaveChanges();
                    }
                }
            }
            catch { }
        }