Esempio n. 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var messageHandler = GetEventHandler();

            _natsIntegration.Subscribe(messageHandler, new List <string> {
                Target
            });
            var inputMessage = String.Empty;

            while (inputMessage != "q")
            {
                Console.WriteLine("Please enter your message below");
                inputMessage = _consoleHandler.ReadConsole();
                if (inputMessage == "q" || inputMessage == String.Empty)
                {
                    continue;
                }

                if (!_natsIntegration.PublishMessage(inputMessage, Target, Sender))
                {
                    Log.Error($"An error occured whne publishing message. Input message: {inputMessage}" +
                              $"Target: {Target}, Sender: {Sender}");
                }
            }

            _natsIntegration.FlushAndUnsubscribe();
        }
        public async Task <IActionResult> SendMessageToUser([FromBody] ChatMessage chatMessage)
        {
            if (chatMessage == null ||
                String.IsNullOrEmpty(chatMessage.From) ||
                String.IsNullOrEmpty(chatMessage.Message) ||
                String.IsNullOrEmpty(chatMessage.To))
            {
                return(BadRequest("Request object is incorrect"));
            }

            if (!_natsIntegration.PublishMessage(chatMessage.Message, chatMessage.To, chatMessage.From))
            {
                return(StatusCode(500));
            }

            return(Ok());
        }