예제 #1
0
        /// <summary>
        /// When connecting to hub adds to user list and sends the info to connected clients
        /// </summary>
        /// <returns></returns>
        public override Task OnConnected()
        {
            var userName = Context.User.Identity.Name;


            if (userName == null || ConnectedUsers.Users.Contains(userName))
            {
                //Tells the client side to disconnect client. Current way of fixing multiple browser tab issue
                Clients.Caller.onConnected(-1);
            }

            else
            {
                //Changes online status to online
                _service.OnlineStatusToggle(userName, true);


                var friendNames = _service.getFriends(userName);

                var friendList = new List <object>();

                //Iterates through the friendNames and checks if they are online in the Connected Users hashset
                foreach (var friend in friendNames)
                {
                    if (ConnectedUsers.Users.Contains(friend))
                    {
                        var newFriend = new
                        {
                            userName = friend,
                            online   = true
                        };
                        friendList.Add(newFriend);
                        Clients.User(friend).onNewUserConnected(userName);
                    }
                    else
                    {
                        var newFriend = new
                        {
                            userName = friend,
                            online   = false
                        };
                        friendList.Add(newFriend);
                    }
                }
                Clients.Caller.onConnected(friendList);


                //Checks notification amount
                var notificationCount = _service.NotificationCount(userName);
                Clients.Caller.notificationCount(notificationCount);



                //Adds new user to client list
                ConnectedUsers.Users.Add(userName);
            }

            return(base.OnConnected());
        }
예제 #2
0
        /// <summary>
        /// Checks for notificatiosn for a user
        /// </summary>
        /// <returns></returns>
        public Task NotificationCheck()
        {
            var userName = Context.User.Identity.Name;

            if (userName != null)
            {
                var notificationCount = _service.NotificationCount(userName);
                return(Clients.Caller.notificationCount(notificationCount));
            }
            return(null);
        }