Example #1
0
        public IActionResult Create(string id)
        {
            var model = new InputMessageViewModel();

            model.GamingHallId = id;
            return(this.View(model));
        }
Example #2
0
        public async Task <IActionResult> CreateAsync(string id, InputMessageViewModel input)
        {
            await this.massageService.AddAsync(input.Sender, input.Content, id);

            return(this.Redirect("/GamingHall/Index/" + id));
        }
        public async Task Send(InputMessageViewModel inputMessage)
        {
            Conversation conversation = await this.context.Conversations
                                        .Include(x => x.UserConversations)
                                        .SingleOrDefaultAsync(x => x.ConversationId == inputMessage.ConversationId);

            if (conversation == null)
            {
                this.logger.LogInformation("Message with no conversation. Cannot be broadcasted.");

                return;
            }

            User owner = await this.context.Users.FindAsync(inputMessage.UserId);

            if (owner == null)
            {
                this.logger.LogWarning("Meesage with no owner. Cannot be broadcasted");

                return;
            }

            var messageDb = new Message
            {
                Content      = inputMessage.Content,
                Conversation = conversation,
                Owner        = owner
            };

            try
            {
                await this.context.AddAsync(messageDb);

                await this.context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                this.logger.LogError("Problem with storing message", ex);
            }

            List <string> addressesClientIds = conversation.UserConversations
                                               .Select(x => x.UserId)
                                               .ToList();

            foreach (var userId in addressesClientIds)
            {
                var addresse = this.context.Users
                               .Include(x => x.HubConnections)
                               .SingleOrDefault(x => x.Id == userId);

                if (addresse != null)
                {
                    foreach (var connection in addresse.HubConnections)
                    {
                        if (connection.Connected)
                        {
                            await this.Clients.Client(connection.HubConnectionId)
                            .SendAsync("broadcastMessage", messageDb.ToFlattenViewModel());
                        }
                    }
                }
            }
        }