public void Disconnect(string connectionId, bool forceDisconnect) { var chat = ChatServices.WebChats.FirstOrDefault(c => c.ConnectionId == connectionId || (c.PreviousConnectionIds != null && c.PreviousConnectionIds.Contains(connectionId))); if (!forceDisconnect) { if (chat != null && ChatController.ContinueChat() && (chat.State == ChatState.Connected || chat.State == ChatState.Paused)) { var now = DateTime.Now; var remaining = ChatController.ContinueChatTimeout(); if (chat.DateEnded.HasValue) { var elapsed = now.Subtract(chat.DateEnded.Value); remaining = ChatController.ContinueChatTimeout() - Convert.ToInt32(elapsed.TotalSeconds); } if (remaining > 0) { var isPaused = chat.State == ChatState.Paused; Clients.Client(connectionId).pauseWcbChat(isPaused); return; } } Clients.Client(connectionId).disconnected(ChatController.EnableKeepOpenOnDisconnectAndStartNew(), true); } else { if (chat != null) { ChatServices.SendCustomSystemMessage(CustomMessageType.VisitorDisconnect, "", chat.ConnectionId, chat); if (chat.PreviousConnectionIds != null && chat.PreviousConnectionIds.Any()) { foreach (var cid in chat.PreviousConnectionIds.Where(p => p != chat.ConnectionId)) { ChatServices.SendCustomSystemMessage(CustomMessageType.VisitorDisconnect, "", cid, chat); } } ChatServices.ProcessDisconnect(chat.ConnectionId, false, true); } } }
void PollChat(WebChat webChat) { if (webChat.State == ChatState.Disconnected) { Disconnect(webChat, ChatServices.DisconnectReason.UserDisconnect); return; } if (webChat.State == ChatState.Paused || (ChatController.KeepQueuedChatsAlive && webChat.State == ChatState.Queued && webChat.IsMinimized)) { if (webChat.DatePaused.HasValue) { var elapsed = DateTime.Now.Subtract(webChat.DatePaused.Value); var elapsedSeconds = Convert.ToInt32(elapsed.TotalSeconds); var continueChatTimeout = ChatController.ContinueChatTimeout(); if (elapsedSeconds > continueChatTimeout) { LoggingService.GetInstance().LogNote(String.Format("Continue Chat timed out for connection: {0}", webChat.ConnectionId)); Disconnect(webChat, ChatServices.DisconnectReason.ContinueChatTimeout); return; } } } if ((webChat.State == ChatState.Connected || webChat.State == ChatState.Paused) && ChatController.EnableInactivityTimeout() && webChat.DateAnswered.HasValue) { if (ChatController.InactivityTimeout() > 0) { var inactiveTimeout = ChatController.InactivityTimeout(); var lastAgent = webChat.Messages.OrderBy(m => m.Order).LastOrDefault(m => m.Direction == "in" && !m.Name.Equals("System", StringComparison.OrdinalIgnoreCase)); var lastUser = webChat.Messages.OrderBy(m => m.Order).LastOrDefault(m => m.Direction == "out" && !m.Name.Equals("System", StringComparison.OrdinalIgnoreCase)); var now = DateTime.Now; var last = now; if (lastAgent != null && lastUser != null) { if (ChatController.EnableInactivityResetOnAgentMessage()) { last = lastAgent.DateSent > lastUser.DateSent ? lastAgent.DateSent : lastUser.DateSent; } else { last = lastUser.DateSent; } } else if (lastAgent != null) { last = lastAgent.DateSent; } if (last != now) { var elapsed = now.Subtract(last); var elapsedSeconds = Convert.ToInt32(elapsed.TotalSeconds); if (elapsedSeconds > inactiveTimeout) { LoggingService.GetInstance().LogNote(String.Format("Inactivity Time Out Occurred: {0}", webChat.ConnectionId)); Disconnect(webChat, ChatServices.DisconnectReason.InactivityTimeout); return; } } } } ChatServices.PollEvents(webChat.ParticipantId, webChat.ConnectionId, webChat.SessionId); }