public async Task SendMessage(string chatRoomId, string message)
        {
            string botChatUser = _userRepository
                                 .Get(new UserSpecification("bot@koalaappchat"))
                                 .FirstOrDefault()?.UserName;

            try {
                ChatUser chatUser = _userRepository
                                    .Get(new UserSpecification(Context.User.Identity.Name))
                                    .FirstOrDefault();
                ChatMessage chatMessage = _messageParser.ParseMessage(chatUser.Id, message);
                await _mediator
                .Send <bool>(new ChatMessageRequestModel {
                    ChatMessage = chatMessage,
                    ChatRoomId  = chatRoomId
                });

                if (chatMessage.MessageType == ApplicationCore.Enums.ChatMessageType.TEXT)
                {
                    await Clients
                    .All
                    .SendAsync(chatRoomId,
                               Context.User.Identity.Name,
                               chatMessage.SentDate.ToString("yyyy-MM-dd HH:mm"),
                               message);
                }
            } catch (CommandFormatException ex) {
                await Clients
                .Client(Context.ConnectionId)
                .SendAsync(chatRoomId,
                           botChatUser,
                           DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm"),
                           $"Command sent ({message}) invalid.");

                _logger.LogError(ex,
                                 $"User {Context.User.Identity.Name} sent an invalid command " +
                                 $"to Chat Room {chatRoomId}. Message sent: {message}.");
            } catch (CommandNotFoundException ex) {
                await Clients
                .Client(Context.ConnectionId)
                .SendAsync(chatRoomId,
                           botChatUser,
                           DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm"),
                           $"Command sent ({message}) not allowed.");

                _logger.LogError(ex,
                                 $"User {Context.User.Identity.Name} sent not allowed command " +
                                 $"to Chat Room {chatRoomId}. Message sent: {message}.");
            } catch (Exception ex) {
                _logger.LogError(ex,
                                 "An internal error occured while processing message sent.");
            }
        }
Exemple #2
0
        public void Demonstration()
        {
            //The amount of code involved in using this component is minimal.
            //More effort is required to set up the configurations correctly.
            //Refer to CS or sample config files in test_files folder for the same

            //Usage:

            //Use static method of MessageParserManager to get a parser instance.
            //Get default instance of parser
            IMessageParser parser = MessageParserManager.GetParser();

            //Or get a custom parser instance as per defined in configuration file, using the overloaded function.
            parser = MessageParserManager.GetParser("XmlMessageParser");

            //Get the message string to parse
            string messageText = File.ReadAllText("../../test_files/testMessage.xml");

            //Get the Message instance
            Message message = parser.ParseMessage(messageText);
        }
        private void OnDataReceived(object sender, DataReceivedArgs args)
        {
            var message = _messageParser.ParseMessage(args.Buffer);

            _upwardMessages.Enqueue(message);
        }
Exemple #4
0
        private UserMessage GetValidMessage(BasicDeliverEventArgs args)
        {
            var message = _parser.ParseMessage <UserMessage>(args);

            return(message);
        }