示例#1
0
        public Agent RegisterAgent(string name, Guid socketID, Guid id)
        {
            var agent = _agentDataModel.Get(id);

            if (agent == null)
            {
                agent              = new Agent();
                agent.ID           = new Guid(id.ToString());
                agent.SocketID     = socketID;
                agent.Username     = name;
                agent.ServiceCount = 0;

                _agentDataModel.Insert(agent);
            }
            else
            {
                agent.IsPaused = false;
                agent.SocketID = socketID;
                _agentDataModel.Update(agent);
            }

            return(agent);
        }
        public async void MonitorQueue()
        {
            do
            {
                var chatQueues = _queueDataModel.Get();

                if (chatQueues != null && chatQueues.Any())
                {
                    var inactiveChats = chatQueues.Values.Where(x => x.CurrentAgent == null);

                    if (inactiveChats.Any())
                    {
                        var agents = _agentDataModel.Get();

                        if (agents != null && agents.Any())
                        {
                            var unservicedChats = inactiveChats.OrderBy(x => x.ChatEntryTime);

                            foreach (var unservicedChat in unservicedChats)
                            {
                                var availableAgents = agents.Values.Where(x => x.ActiveChats.Count() < x.MaxChats && x.IsPaused == false);
                                if (availableAgents.Any())
                                {
                                    var leastActiveAgent = availableAgents.OrderBy(x => x.ServiceCount).FirstOrDefault();
                                    unservicedChat.CurrentAgent = leastActiveAgent.ID;
                                    unservicedChat.ExpiryTime   = DateTime.UtcNow.AddMinutes(Constants.DefaultChatExpiry);

                                    leastActiveAgent.ServiceCount += 1;
                                    leastActiveAgent.ActiveChats.Add(unservicedChat.ClientID);

                                    _agentDataModel.Update(leastActiveAgent);
                                    _queueDataModel.Update(unservicedChat);

                                    _chatHub.StartChat(unservicedChat.ClientID.ToString(), leastActiveAgent.ID.ToString());
                                }
                            }
                        }
                    }
                }

                await Task.Delay(10000);
            } while (true);
        }