示例#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();
                string _fromUserId      = "";
                _fromUserId = strfromUserId;
                string _toUserId = "";
                _toUserId = toUserId;
                List <UserDetail> FromUsers = ConnectedUsers.Where(u => u.UserID == _fromUserId).ToList();
                List <UserDetail> ToUsers   = ConnectedUsers.Where(x => x.UserID == _toUserId).ToList();
                ChatMessage       chat      = new ChatMessage();

                if (FromUsers.Count != 0 && ToUsers.Count() != 0)
                {
                    foreach (var ToUser in ToUsers)
                    {
                        chat.Body       = message;
                        chat.Time_Stamp = DateTime.Now;
                        chat.toUserId   = toUserId;
                        // 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
                        chat.fromUserId = FromUser.UserID;
                        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
                    };
                    db.ChatMessages.Add(chat);
                    db.SaveChanges();
                    AddMessageinCache(_MessageDeail);
                }
            }
            catch { }
        }