public ChatResult AddMessage(string connectionId, MessageInfo msg) { if (m_mapConnectionIdCustomer.ContainsKey(connectionId)) { string myCustomerId = m_mapConnectionIdCustomer[connectionId]; string receiverId = msg.ReceiverId; //nếu receiver ở đây là customer(ko phải group) thì kiểm tra customer on hay off if (m_customerInfoDict.ContainsKey(receiverId)) { //nếu receiver offline thì lưu vào OffLineMessageDict string status = m_customerInfoDict[receiverId].Status; if (status.Equals(ChatDefinition.Offline)) { if (!m_offlineMessage.ContainsKey(receiverId)) m_offlineMessage.Add(receiverId, new Dictionary<string, MessageInfo>()); if (!m_offlineMessage[receiverId].ContainsKey(msg.Id)) m_offlineMessage[receiverId].Add(msg.Id, msg); m_adapter.AddOfflineMessage(myCustomerId, msg.Id);//database } } //nếu receiver ở đây là Group thì kiểm tra group tồn tại ko else { if (!m_groupInfoDict.ContainsKey(receiverId) && !receiverId.Equals("All")) return new ChatResult(false, "ReceiverId doesn't exist."); } if (!m_messageDict.ContainsKey(myCustomerId)) m_messageDict.Add(myCustomerId, new Dictionary<string, MessageInfo>()); if (!m_messageDict[myCustomerId].ContainsKey(msg.Id)) m_messageDict[myCustomerId].Add(msg.Id, msg); m_adapter.AddMessage(myCustomerId, msg.Id);//database return new ChatResult(true, null); } return new ChatResult(false, "Your ID doens't exist."); }
public ChatResult SendMessageToFriend(MessageInfo msg) { ChatResult result = Manager.Instance.AddMessage(Context.ConnectionId, msg); if (result.Success) { CustomerInfo myCustomer = Manager.Instance.GetCustomerByConnectionId(Context.ConnectionId); CustomerInfo friendCustomer = Manager.Instance.GetCustomerByCustomerId(msg.ReceiverId); //send message to friend List<string> friendConnectionIds = friendCustomer.ConnectionList; Clients.Clients(friendConnectionIds).OnReceiveMessage(msg); //send message to myCustomer(#connectionId) List<string> myConnectionIds = new List<string>(myCustomer.ConnectionList); myConnectionIds.Remove(Context.ConnectionId); Clients.Clients(myConnectionIds).OnReceiveMessage(msg); } return result; }
public ChatResult SendMessageToGroup(MessageInfo msg) { ChatResult result = Manager.Instance.AddMessage(Context.ConnectionId, msg); if (result.Success) { GroupInfo group = GetGroupByGroupId(msg.ReceiverId); string senderId = msg.SenderId; List<string> customerConnectionIds = new List<string>(); foreach (string customerId in group.CustomerList) customerConnectionIds.AddRange(Manager.Instance.GetCustomerByCustomerId(customerId).ConnectionList); customerConnectionIds.Remove(Context.ConnectionId); Clients.Clients(customerConnectionIds).OnReceiveMessage(msg); } return result; }
public ChatResult BroadcastMessage(MessageInfo message) { CustomerInfo customer = Manager.Instance.GetCustomerByConnectionId(Context.ConnectionId); if (customer != null) { Clients.AllExcept(Context.ConnectionId).OnReceiveMessage(message); return new ChatResult(true, null); } return new ChatResult(false, "Your Id doesn't exist."); }