Example #1
0
        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);
                }
            }
        }