示例#1
0
 public override Task OnConnected()
 {
     Models.User user;
     if (AuthorizedUser(out user, Context.ConnectionId))
     {
         _connections.Add(user.Id, user.ConnectionInfo);
         DataClassesManager.InsertLog(string.Empty, "SignalR", "OnConnected", Context.ConnectionId, user.Id);
     }
     return(base.OnConnected());
 }
示例#2
0
        protected override void OnIncomingError(ExceptionContext exceptionContext, IHubIncomingInvokerContext invokerContext)
        {
            var connectionId = invokerContext.Hub.Context.ConnectionId;

            DataClassesManager.InsertLog("=> Exception " + exceptionContext.Error.Message, "SignalR", "OnIncomingError", connectionId);
            if (exceptionContext.Error.InnerException != null)
            {
                DataClassesManager.InsertLog("=> Inner Exception " + exceptionContext.Error.InnerException.Message, "SignalR", "OnIncomingError", connectionId);
            }
            base.OnIncomingError(exceptionContext, invokerContext);
        }
示例#3
0
        public void ConfirmDelivery(IList <string> receivedIds)
        {
            Models.User user;
            if (AuthorizedUser(out user))
            {
                DataClassesManager.InsertLog(string.Join(",", receivedIds), "SignalR", "ConfirmDelivery", Context.ConnectionId, string.Format("{0}-{1}", user.UserId, user.Username));

                var updates = DataClassesManager.NotificationDelivered(user.UserId, string.Join(",", receivedIds));
                foreach (var update in updates)
                {
                    var recipientConnectionId = _connections.GetConnectionId(update.Recipient_Id);
                    if (!string.IsNullOrEmpty(recipientConnectionId))
                    {
                        Clients.Client(recipientConnectionId).MyMessageDelivered(update.Id, update.Content);
                    }
                }
            }
        }
示例#4
0
 public override Task OnDisconnected(bool stopCalled)
 {
     if (!stopCalled)
     {
         var id = _connections.GetUserIdByConnectionId(Context.ConnectionId);
         _connections.Remove(id);
         DataClassesManager.InsertLog(string.Empty, "SignalR", "OnDisconnected", Context.ConnectionId, id);
     }
     else
     {
         Models.User user;
         if (AuthorizedUser(out user))
         {
             _connections.Remove(user.Id);
             DataClassesManager.InsertLog(string.Empty, "SignalR", "OnDisconnected", Context.ConnectionId, string.Format("{0}-{1}", user.UserId, user.Username));
         }
     }
     return(base.OnDisconnected(stopCalled));
 }
示例#5
0
        public void ConfirmRead(string notificationId, bool applyOnPreviouslyReceived)
        {
            Models.User user;
            if (AuthorizedUser(out user))
            {
                DataClassesManager.InsertLog(string.Format("{0}-{1}", notificationId, applyOnPreviouslyReceived.ToString()), "SignalR", "ConfirmRead", Context.ConnectionId, string.Format("{0}-{1}", user.UserId, user.Username));

                string recipientId = string.Empty;
                var    update      = DataClassesManager.NotificationRead(user.UserId, notificationId, applyOnPreviouslyReceived);
                if (update != null)
                {
                    var recipientConnectionId = _connections.GetConnectionId(update.Recipient_Id);
                    if (!string.IsNullOrEmpty(recipientConnectionId))
                    {
                        Clients.Client(recipientConnectionId).MyMessageRead(update.Id, update.Content, applyOnPreviouslyReceived);
                    }
                }
            }
        }
示例#6
0
        private string SendMessage(string recipientId, string localMessageId, int messageTypeId, string message, string expiresAt, bool isScrambled)
        {
            Models.User sender;

            var messageId             = string.Empty;
            var issuedAt              = string.Empty;
            var reInitSessionRequired = false;

            if (AuthorizedUser(out sender))
            {
                //if (!ChatHelper.FormatMessage(messageTypeId, ref message))
                //    return string.Empty;

                var recipientConnection = _connections.GetConnection(recipientId);

                long recipientInternalId = recipientConnection.PushMode ? -1 : recipientConnection.UserId;

                var feedback = ChatUtils.SP_SaveNotification(sender.UserId, recipientId, ref recipientInternalId, localMessageId, messageTypeId, message, isScrambled, expiresAt);
                DataClassesManager.InsertLog(string.Format("From {0}:{1}", sender.Username, message), "ChatHub", "SendMessage", string.Format("{0}-{1}", recipientConnection.PushMode, recipientConnection.ConnectionId), recipientId);
                messageId             = feedback.Id;
                issuedAt              = feedback.FormattedIssuedAt;
                reInitSessionRequired = feedback.ReInitSessionRequired;
                if (string.IsNullOrEmpty(messageId))
                {
                    return(string.Empty);
                }

                if (feedback.AlreadyReceived)
                {
                    goto AlreadyReceived;
                }

                if (!recipientConnection.PushMode)
                {
                    if (!string.IsNullOrEmpty(expiresAt))//.HasValue
                    {
                        Clients.Client(recipientConnection.ConnectionId).TemporaryMessageReceived(messageTypeId, messageId, message, sender.Id, expiresAt, isScrambled, issuedAt);
                    }
                    else
                    {
                        Clients.Client(recipientConnection.ConnectionId).MessageReceived(messageTypeId, messageId, message, sender.Id, isScrambled, issuedAt);
                    }
                }
                else
                {
                    bool requirePush; string pushPreview;
                    //requirePush = Settings.PushNotificationTypes.TryGetValue(messageTypeId, out pushPreview);
                    //if (string.IsNullOrEmpty(pushPreview))
                    //    pushPreview = message;
                    //Task.Factory.StartNew(() => {
                    //    PushManager.Send(pushPreview, recipientInternalId, recipientId, sender.Name, sender.Id);
                    //});
                }
AlreadyReceived:
                if (messageId == "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")
                {
                    Clients.Client(sender.ConnectionInfo.ConnectionId).RemoveContact(Guid.NewGuid().ToString(), recipientId);
                    messageId = Guid.NewGuid().ToString();
                }
                var callBack = string.Empty;
                callBack = string.Format("{0}#{1}#{2}#{3}", localMessageId, messageId, issuedAt, reInitSessionRequired.ToString());
                return(callBack);
            }
            throw new HubException("Reconnect required");
        }
示例#7
0
 public static void Remove(string id)
 {
     _connections.Remove(id);
     DataClassesManager.InsertLog(string.Empty, "SignalR", "#OnDisconnected", "", id);
 }