예제 #1
0
        public void SendNotification(IEnumerable <Guid> users, Notification.Entity.SignalR.Notification notification)
        {
            try
            {
                LogBusiness.Info(string.Format("[NotificationHub] SendNotification (connectionId: {0})", Context.ConnectionId));

                if (users == null || !users.Any())
                {
                    LogBusiness.Warn(string.Format("[NotificationHub] SendNotification  - Listagem de usuários vazia (connectionId: {0})", Context.ConnectionId));
                    return;
                }

                if (notification == null)
                {
                    LogBusiness.Warn(string.Format("[NotificationHub] SendNotification  - Notificação vazia (connectionId: {0})", Context.ConnectionId));
                    return;
                }

                foreach (var user in users)
                {
                    LogBusiness.Debug(string.Format("[NotificationHub] SendNotification  - Notificação ({0}) enviada para o usuário ({1})", notification.Id, user));
                    Clients.Group(user.ToString()).ReceiveNotification(notification);
                }
            }
            catch (Exception exc)
            {
                LogBusiness.Error(exc);
                throw;
            }
        }
        public static void SendNotification(IEnumerable <Guid> users, Guid notificationId)
        {
            try
            {
                var notif = NotificationBusiness.GetById(notificationId);

                if (notif == null)
                {
                    LogBusiness.Warn(string.Format("[SignalRClientBusiness] SendNotification - Notificação não encontrada ({0})", notificationId));
                    return;
                }

                if ((notif.DateStartNotification > DateTime.Now.Date) ||
                    (notif.DateEndNotification != null && DateTime.Now.Date > notif.DateEndNotification))
                {
                    LogBusiness.Info(string.Format("[SignalRClientBusiness] SendNotification - Notificação fora do período programado ({0})", notificationId));
                    return;
                }

                var notifP = new Notification.Entity.SignalR.Notification()
                {
                    Id      = notif.Id,
                    Title   = notif.Title,
                    Message = notif.Message
                };

                var hubConnection = new HubConnection(UrlSignalRServer);
                var hub           = hubConnection.CreateHubProxy("notificationHub");

                hubConnection.Headers.Add("Authorization", "Basic " + Base64Encode(UserCredentialSignalRServer, PasswordCredentialSignalRServer));

                hubConnection.Start().Wait();

                int i     = 0;
                int total = users.Count();

                while (i < total)
                {
                    var ltU = users.Skip(i).Take(1000).ToList();
                    LogBusiness.Debug(string.Format("[SignalRClientBusiness] SendNotification - Enviando... ({0}-{1})", i, ltU.Count));
                    hub.Invoke("SendNotification", ltU, notifP).Wait();
                    i += 1000;
                }

                hubConnection.Stop();
            }
            catch (Exception exc)
            {
                LogBusiness.Error(exc);
                throw;
            }
        }