예제 #1
0
파일: Chat.cs 프로젝트: Herhangi/HerhangiOT
        public static bool RemoveUserFromChannel(Player player, ushort channelId)
        {
            ChatChannel channel = GetChannel(player, channelId);

            if (channel == null || !channel.RemoveUser(player))
            {
                return(false);
            }

            if (channel.Owner == player.CharacterId)
            {
                DeleteChannel(player, channelId);
            }

            return(true);
        }
예제 #2
0
        public bool UnregisterChat(HubCallerContext hubCallerContext, string channelId)
        {
            try
            {
                HubContext.Groups.Remove(hubCallerContext.ConnectionId, channelId);

                long accountId = GetAccountId(hubCallerContext);

                if (accountId < 1)
                {
                    return(false);
                }

                ChatChannel chatChannel = GetChannel(channelId);
                if (chatChannel != null)
                {
                    ChatUser chatUser = GetUser(accountId);
                    if (chatUser != null)
                    {
                        chatUser.SetActive(false);
                        NLogManager.LogMessage(string.Format("User leave channel: {0}:{1} - ChannelId={2}", chatUser.AccountID, chatUser.UserName, channelId));
                    }

                    //neu phong chat it hon 20 nguoi khong remove user khoi danh sach onlines (fake so luong user online)
                    if (chatChannel.UserOnlines.Count > MIN_USER_INACTIVE_IN_CHANNEL)
                    {
                        chatChannel.RemoveUser(accountId);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
            }
            return(false);
        }
예제 #3
0
        private void RemoveInactive(object obj)
        {
            try
            {
                //Xóa các user inactive trong 10p khỏi bộ nhớ hệ thống
                if (UserOnlines != null && UserOnlines.Count > 0)
                {
                    //Những user có kênh chat inactive trong 10p
                    DateTime now = DateTime.Now;
                    ConcurrentDictionary <long, ChatUser> list = new ConcurrentDictionary <long, ChatUser>(UserOnlines.Where(_ => _.Value != null && _.Value.LastActivity.AddSeconds(MAX_IDLE_USER_ONLINE) < now));//((!_.Value.IsActive && _.Value.LastMessageSentTime.AddSeconds(TIME_INTERVAL * 3) < now) || _.Value.LastActivity.AddSeconds(MAX_IDLE_USER_ONLINE) < now)));
                    if (list != null && list.Count > 0)
                    {
                        foreach (ChatUser chatUser in list.Values)
                        {
                            foreach (string channelId in chatUser.ChannelConnectionIds.Keys)
                            {
                                string oldConnectionId;
                                if (chatUser.RemoveChannelConnectionId(channelId, out oldConnectionId))
                                {
                                    HubContext.Groups.Remove(oldConnectionId, channelId);
                                }

                                ChatChannel chatChannel = GetChannel(channelId);
                                if (chatChannel != null)
                                {
                                    NLogManager.LogMessage(string.Format("ChatChannel --> RemoveUser: {0} - {1}:{2}", channelId, chatUser.AccountID, chatUser.UserName));
                                    chatChannel.RemoveUser(chatUser.AccountID);
                                }
                            }

                            if (chatUser.ChannelConnectionIds.Keys.Count == 0)
                            {
                                if (Monitor.TryEnter(_lockUser, _lockTime))
                                {
                                    try
                                    {
                                        ChatUser outChatUser = null;
                                        UserOnlines.TryRemove(chatUser.AccountID, out outChatUser);

                                        if (outChatUser != null)
                                        {
                                            ConcurrentDictionary <string, long> listConn = new ConcurrentDictionary <string, long>(ConnectionIdAccountId.Where(__ => __.Value == chatUser.AccountID));
                                            if (listConn != null && listConn.Count > 0)
                                            {
                                                long accId;
                                                foreach (string connId in listConn.Keys)
                                                {
                                                    ConnectionIdAccountId.TryRemove(connId, out accId);
                                                }
                                            }

                                            NLogManager.LogMessage(string.Format("InActive User: {0} - {1}", outChatUser.AccountID, outChatUser.UserName));
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        NLogManager.PublishException(e);
                                    }
                                    finally
                                    {
                                        Monitor.Exit(_lockUser);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                NLogManager.LogMessage("Inactive 1");
                NLogManager.PublishException(e);
            }

            try
            {
                //xóa các kênh chat không hoạt động
                if (Channels != null && Channels.Count > 0)
                {
                    ConcurrentDictionary <string, ChatChannel> list = new ConcurrentDictionary <string, ChatChannel>(Channels.Where(_ => _.Value != null && _.Value.UserOnlines.Count == 0));
                    if (list != null && list.Count > 0)
                    {
                        foreach (ChatChannel chatChannel in list.Values)
                        {
                            if (Monitor.TryEnter(_lockChannel, _lockTime))
                            {
                                try
                                {
                                    ChatChannel outChatChannel = null;
                                    Channels.TryRemove(chatChannel.ChannelId, out outChatChannel);
                                    NLogManager.LogMessage(string.Format("Remove ChatChannel: {0}", chatChannel.ChannelId));
                                }
                                catch (Exception e)
                                {
                                    NLogManager.PublishException(e);
                                }
                                finally
                                {
                                    Monitor.Exit(_lockChannel);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                NLogManager.LogMessage("Inactive 2");
                NLogManager.PublishException(e);
            }
        }