private void RegisterChannelController()
        {
            channelControllerModel = MessagingFactory.GetConnection().CreateModel();
            var name = "channel-controller";

            channelControllerModel.QueueDeclare(queue: name,
                                                durable: true,
                                                exclusive: false,
                                                autoDelete: false,
                                                arguments: null);

            var consumer = new EventingBasicConsumer(channelControllerModel);

            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body.ToArray();
                var message = Encoding.UTF8.GetString(body);
                var result  = JsonConvert.DeserializeObject <ChannelControllerMessage>(message);
                _log.Information("[{0}] New ChannelControllerMessage Received", result.ChannelId);

                OnChannelControllerMessage?.Invoke(this, result);
            };
            channelControllerModel.BasicConsume(queue: name,
                                                autoAck: true,
                                                consumer: consumer);
        }
Example #2
0
        public async Task <IActionResult> Stop(Guid channelId)
        {
            using (var ctx = DataFactory.GetDataContext())
            {
                var data = await ctx.Channel.FirstOrDefaultAsync(c => c.OwnerId == User.GetUserId() && c.Id == channelId && !c.IsDisabled);

                if (data == null)
                {
                    return(NotFound());
                }
                else
                {
                    _log.Information("[{0}] Sending stop message to channel", channelId);
                    using (var channel = MessagingFactory.GetConnection().CreateModel())
                    {
                        var name = "channel-controller";
                        channel.QueueDeclare(queue: name,
                                             durable: true,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);

                        var model = new ChannelControllerMessage()
                        {
                            ChannelId = channelId,
                            Action    = ChannelControllerMessageAction.STOP
                        };

                        string message = model.ToJson();
                        var    body    = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(exchange: "",
                                             routingKey: name,
                                             basicProperties: null,
                                             body: body);
                    }

                    data.Status = "STOPPED";
                    await ctx.SaveChangesAsync();

                    return(Ok());
                }
            }
        }
Example #3
0
        static async Task Main(string[] args)
        {
            IConnection messaging;
            IModel      model;
            var         col = new ServiceCollection();

            try
            {
                col.AddMessaging("amqp://*****:*****@localhost");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to Start MessagingService");
            }
            var sp = col.BuildServiceProvider();

            try
            {
                messaging = MessagingFactory.GetConnection();
                model     = messaging.CreateModel();
            }
            catch (Exception _) { }

            DataFactory.Initialize(sp, "server=database-1.co098l7654et.sa-east-1.rds.amazonaws.com;port=3306;user=admin;password=xpLpP5cLkAAXaRrkxz0O;database=hotsapp;TreatTinyAsBoolean=false");

            using (var conn = DataFactory.OpenConnection())
            {
                try
                {
                    var t = conn.Query("SELECT NOW()", new { id = 1 });
                    var b = 1;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            Console.ReadLine();
        }
 public static ChannelConnection CreateChannel()
 {
     return(new ChannelConnection(MessagingFactory.GetConnection()));
 }
 private void RegisterPlayEvent()
 {
     playEventModel = MessagingFactory.GetConnection().CreateModel();
     playEventModel.ExchangeDeclare("channel-playevent", ExchangeType.Topic);
 }