Exemplo n.º 1
0
        public IActionResult Chat(ChatModel model)
        {
            var outModel = new ChatModel();

            // Check if we have the ChatEntries queue initialized
            var chatEntriesQueue = HttpContext.Session.Get <Queue <ChatEntry> >("ChatEntriesQueue");

            if (chatEntriesQueue == null)
            {
                // Initialize the queue
                chatEntriesQueue = new Queue <ChatEntry>();
            }

            if (ModelState.IsValid)
            {
                // Add the entry to the queue
                var newChat = new ChatEntry {
                    DatePosted = DateTime.UtcNow, Text = model.ChatText, WhoPosted = User.Identity.Name
                };
                _model.BasicPublish(ExchangeName, "chat", null, newChat.Serialize());
            }

            // Connect to our rabbit queue and retrieve messages
            _model.BasicQos(prefetchSize: 0, prefetchCount: 10, global: false);
            var queueName = $"Chat_{User.Identity.Name}_Queue";
            var _consumer = new QueueingBasicConsumer(_model);

            _model.BasicConsume(queue: queueName, autoAck: false, _consumer);
            while (true)
            {
                if (!_consumer.Queue.Dequeue(millisecondsTimeout: 100, out var result))
                {
                    break;
                }
                var message = (ChatEntry)result.Body.DeSerialize(typeof(ChatEntry));
                // Check for stock commands
                if (!message.Text.StartsWith("/stock=", StringComparison.InvariantCultureIgnoreCase))
                {
                    chatEntriesQueue.Enqueue(new ChatEntry {
                        DatePosted = message.DatePosted, Text = message.Text, WhoPosted = message.WhoPosted
                    });
                }
                // Acknowledge
                _model.BasicAck(result.DeliveryTag, false);
            }

            // Take the last 50 entries of the local queue
            outModel.ChatEntries = chatEntriesQueue.TakeLast(50).ToList();
            // Remove old ones
            while (chatEntriesQueue.Count > 50)
            {
                chatEntriesQueue.Dequeue();
            }
            // Add the local queue back to the session
            HttpContext.Session.Set <Queue <ChatEntry> >("ChatEntriesQueue", chatEntriesQueue);

            return(View(outModel));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Send a message to the exchange with a stock quote
 /// </summary>
 /// <param name="channel">Channel</param>
 /// <param name="symbol">Stock symbol</param>
 /// <param name="quote">Stock Quote value</param>
 private static void PostStockQuoteAsBot(IModel channel, string symbol, string quote)
 {
     try
     {
         var message = new ChatEntry {
             DatePosted = DateTime.UtcNow, Text = $"{symbol} quote is ${quote} per share", WhoPosted = BotName
         };
         channel.BasicPublish(ExchangeName, "bot", null, message.Serialize());
         Console.WriteLine($"Bot Sent {message.Text}");
     }
     catch (Exception ex)
     {
         // Nothing to do here
         Console.WriteLine($"The bot had an issue posting to the queue: {ex.Message}");
     }
 }
Exemplo n.º 3
0
 private static void SendMessage(ChatEntry message)
 {
     _model.BasicPublish(ExchangeName, "chat", null, message.Serialize());
     Console.WriteLine($"Message Sent by {message.WhoPosted}: {message.Text}");
 }