Example #1
0
 public Task AddMessageAsync(Message message)
 {
     try
     {
         IChatService proxy = ServiceProxy.Create <IChatService>(this.defaultPartitionID, chatServiceInstance);
         return(proxy.AddMessageAsync(message));
     }
     catch (Exception e)
     {
         ServiceEventSource.Current.Message("AddMessageAsync failed because of error {0}", e.ToString());
         throw;
     }
 }
Example #2
0
 public Task AddMessageAsync(Message message)
 {
     try
     {
         // The service only has a single partition. In order to access the partition need to provide a
         // value in the LowKey - HighKey range defined in the ApplicationManifest.xml
         IChatService proxy = ServiceProxy.Create <IChatService>(chatServiceInstance, new ServicePartitionKey(1));
         return(proxy.AddMessageAsync(message));
     }
     catch (Exception e)
     {
         ServiceEventSource.Current.Message("AddMessageAsync failed because of error {0}", e.ToString());
         throw;
     }
 }
Example #3
0
        public async Task SendMessage(Message message)
        {
            var recipient = await _userService.GetUserByIdAsync(message.RecipientId);

            var sender = await _userService.GetUserByUserNameAsync(Context.User.Identity.Name);

            var senderDetail = GetUserDetail(sender.Id);

            if (senderDetail != null)
            {
                if (!await _chatService.DoesChatExistAsync(sender.Id))
                {
                    var chat = new Chat
                    {
                        ChatId = sender.Id
                    };

                    await _chatService.AddChatAsync(chat);
                }

                message.ChatId = sender.Id;

                await _chatService.AddMessageAsync(message);

                await _unitOfWork.CompleteAsync();

                var senderChatModel = PrepareChatModel(message, sender, MessagePosition.Right, "me");

                var recipientChatModel = PrepareChatModel(message, sender, MessagePosition.Left);

                var senderMessageTemplate = await PrepareMessageTemplate(senderChatModel);

                var recipientMessageTemplate = await PrepareMessageTemplate(recipientChatModel);

                //Send message to the recipient if and only if is online
                if (GetUserDetail(recipient.Id) != null)
                {
                    await Clients.User(recipient.Id)
                    .SendAsync("addChatMessage", recipientMessageTemplate, message.Id, sender.Id);
                }

                //Caller
                await Clients.User(sender.Id).SendAsync("addChatMessage", senderMessageTemplate, message.Id, null);
            }
        }