コード例 #1
0
        public static void SendStatusNotification(string userId, string title, string message, NotificationType notifyType)
        {
            //
            // TODO
            //

            var context        = GlobalHost.ConnectionManager.GetHubContext <NotifyHub>();
            var connectionRepo = new ConnectionsRepository();

            string json = System.Web.Helpers.Json.Encode(new
            {
                title   = title,
                message = message,
                type    = notifyType,
            });


            // Calls method on client side to show notification
            //context.Clients.All.addNotification(json);

            var userConnections = connectionRepo.GetConnectionsByUserId(userId);

            foreach (var connId in userConnections)
            {
                // Calls method on client side to show notification
                context.Clients.Client(connId.RowKey).addNotification(json);
            }
        }
コード例 #2
0
        public void SendNotification()
        {
            var name = Context.User.Identity.Name;

            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            //
            // Azure table storage repositories
            //
            var connRepository = new ConnectionsRepository();
            var notifyRepo     = new NotificationsRepository();

            if (!notifyRepo.Init())
            {
                return;
            }
            if (!connRepository.Init())
            {
                return;
            }

            var connections   = connRepository.GetConnectionsByUserId(name).ToList();
            var notifications = notifyRepo.GetNotificationsByUserId(name).ToList();

            notifications.Sort();

            if (connections.Count > 0 && notifications.Count > 0)
            {
                // Send notification(s) to each user connection.
                foreach (var connection in connections)
                {
                    foreach (var notification in notifications)
                    {
                        string json = System.Web.Helpers.Json.Encode(new
                        {
                            title          = notification.Title,
                            message        = notification.Message,
                            url            = string.IsNullOrEmpty(notification.Url) ? string.Empty : notification.Url,
                            linkText       = string.IsNullOrEmpty(notification.LinkText) ? string.Empty : notification.LinkText,
                            type           = (int)notification.NotifyTypeInt,
                            notificationId = notification.RowKey
                        });

                        // Calls method on client side to show notification
                        Clients.Client(connection.RowKey).addNotification(json);
                    }
                }
            }
        }
コード例 #3
0
        public override Task OnDisconnected(bool stopCalled)
        {
            string name = Context.User.Identity.Name;

            var repository = new ConnectionsRepository();

            if (repository.Init())
            {
                RemoveConnection(name, Context.ConnectionId, repository);
            }

            return(base.OnDisconnected(stopCalled));
        }
コード例 #4
0
        public void RemoveConnection(string name, string connectionId, ConnectionsRepository repo)
        {
            if (!repo.Init())
            {
                return;
            }

            if (!string.IsNullOrEmpty(name))
            {
                repo.RemoveConnection(name, connectionId);

                // Just in case user has old connections in table
                RemoveOldUserConnections(name, 1, repo);

                // TODO testing only
                //var connections = repo.GetAllConnections();
            }
        }
コード例 #5
0
        public override Task OnConnected()
        {
            var name = Context.User.Identity.Name;

            if (!string.IsNullOrEmpty(name))
            {
                var repository = new ConnectionsRepository();
                if (repository.Init())
                {
                    var connection = new ConnectionModel(name, Context.ConnectionId, DateTime.Now);

                    repository.AddConnection(connection);

                    // TODO testing only
                    //var connections = repository.GetAllConnections();
                }
            }

            return(base.OnConnected());
        }
コード例 #6
0
        public void RemoveOldUserConnections(string who, int daysOld, ConnectionsRepository repo)
        {
            if (!repo.Init())
            {
                return;
            }

            var connections = repo.GetConnectionsByUserId(who).ToList();

            var timeSpan = TimeSpan.FromDays(daysOld);

            var dateTimeOffset = DateTime.Now.Subtract(timeSpan);

            foreach (var connection in connections)
            {
                // Remove connection if older that 'daysOld'
                if (connection.ConnectionTimeStamp <= dateTimeOffset)
                {
                    RemoveConnection(connection.PartitionKey, connection.RowKey, repo);
                }
            }
        }