Exemplo n.º 1
0
        public void OnDisconnected(HubCallerContext hubCallerContext)
        {
            long accountId = GetAccountId(hubCallerContext);

            if (accountId < 1)
            {
                return;
            }

            try
            {
                ChatUser chatUser = GetUser(accountId);
                if (Monitor.TryEnter(_lockUser, _lockTime))
                {
                    if (chatUser != null)
                    {
                        chatUser.SetActive(false);
                    }
                }
            }
            finally
            {
                Monitor.Exit(_lockUser);
            }

            //group theo accountId để gửi message cho toàn bộ các connectionId cùng accountId
            //HubContext.Groups.Remove(hubCallerContext.ConnectionId, accountId.ToString());
        }
Exemplo n.º 2
0
        public void OnConnected(HubCallerContext hubCallerContext)
        {
            long accountId = GetAccountId(hubCallerContext);

            NLogManager.LogMessage("OnConnected: " + accountId);

            if (accountId < 1)
            {
                return;
            }

            if (!Monitor.TryEnter(_lockUser, _lockTime))
            {
                return;
            }

            try
            {
                ChatUser chatUser = GetUser(accountId);
                if (chatUser != null)
                {
                    chatUser.SetActive(true);
                }
            }
            finally
            {
                Monitor.Exit(_lockUser);
            }

            //group theo accountId để gửi message cho toàn bộ các connectionId cùng accountId
            //HubContext.Groups.Add(hubCallerContext.ConnectionId, accountId.ToString());
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }