예제 #1
0
        /// <summary>
        /// Đăng ký kênh chat
        /// </summary>
        /// <param name="hubCallerContext"></param>
        /// <param name="channelId"></param>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public bool RegisterChat(HubCallerContext hubCallerContext, string channelId, string nickName = "")
        {
            try
            {
                HubContext.Groups.Add(hubCallerContext.ConnectionId, channelId);

                long accountId = GetAccountId(hubCallerContext);

                ChatChannel chatChannel = GetChannel(channelId, true);
                if (accountId < 1)
                {
                    ClientListLastMessages(hubCallerContext, chatChannel);
                    return(false);
                }

                ChatUser chatUser = GetUser(accountId, channelId, true);
                //var dataUser = AbstractDAOFactory.Instance().CreateMessageDao().SP_Account_Get_Infor(int.Parse(accountId.ToString()));
                //if (dataUser != null)
                //{
                //    nickName = string.IsNullOrEmpty(dataUser.NickName) ? StringUtil.MaskUserName(chatUser.UserName) : dataUser.NickName;
                //}
                //else
                //{
                //    if (string.IsNullOrEmpty(nickName))
                //        nickName = StringUtil.MaskUserName(chatUser.UserName);
                //}
                nickName = AccountSession.AccountName;
                if (nickName.Trim() == "System" || nickName.Trim() == "system")
                {
                    return(false);
                }
                chatUser.NickName = nickName;
                chatUser.SetActive(true);
                chatUser.SetChannelConnectionId(channelId, hubCallerContext.ConnectionId);

                chatChannel.AddUser(chatUser);
                SetCacheLevel(chatUser.UserName, (int)chatUser.AccountID, channelId);
                NLogManager.LogMessage(string.Format("User join channel: {0}:{1} - ChannelId={2}", chatUser.AccountID, chatUser.UserName, channelId));

                ClientListUserOnlines(hubCallerContext, chatChannel);
                ClientListLastMessages(hubCallerContext, chatChannel);
                ClientAddUserOnline(channelId, chatUser);

                #region Fake user in channel chat
                ThreadPool.QueueUserWorkItem(__ =>
                {
                    int countUserOnline = chatChannel.UserOnlines.Count;
                    //neu it them mot user fake
                    if (countUserOnline < ENABLE_FAKENAME_LV1_IN_CHANNEL)
                    {
                        Thread.Sleep(3000);
                        chatChannel.AddFakeUser();
                        //neu it qua them mot user fake nua
                        if (countUserOnline < ENABLE_FAKENAME_LV2_IN_CHANNEL)
                        {
                            Thread.Sleep(2000);
                            chatChannel.AddFakeUser();
                        }
                    }
                    else if (countUserOnline > MIN_USER_INACTIVE_IN_CHANNEL)
                    {
                        chatChannel.RemoveFakeUser();
                    }
                });
                #endregion

                return(true);
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
            }
            return(false);
        }