Beispiel #1
0
        public async Task MessageWaiting()
        {
            using (var channel = _connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "messages_result", type: "direct", true);
                var queueName = channel.QueueDeclare().QueueName;

                channel.QueueBind(queue: queueName,
                                  exchange: "messages_result",
                                  routingKey: "messages_result");

                var consumer = new AsyncEventingBasicConsumer(channel);

                channel.BasicConsume(queueName,
                                     autoAck: true,
                                     consumer: consumer);

                consumer.Received += async(o, a) =>
                {
                    string queueResult = Encoding.UTF8.GetString(a.Body.ToArray());
                    Console.WriteLine(Encoding.UTF8.GetString(a.Body.ToArray()));

                    MessageResultModel messageResult = JsonSerializer.Deserialize <MessageResultModel>(queueResult);

                    _dbClient.PostChanges(messageResult.Id, messageResult.Status);

                    await Task.Yield();
                };

                while (true)
                {
                    await Task.Delay(1000);
                }
            }
        }
Beispiel #2
0
        public void SendMessage(string message)
        {
            MessageModel  messageModel = JsonSerializer.Deserialize <MessageModel>(message);
            SMSResultEnum resultEnum;

            string phone = _apiConsumer.GetPhone(messageModel.ReceiverId);

            if (!String.IsNullOrEmpty(phone))
            {
                SmsModel sms = new SmsModel
                {
                    Phone = phone,
                    Text  = messageModel.Template
                };

                resultEnum = _apiConsumer.PostMessage(sms);
            }
            else
            {
                resultEnum = SMSResultEnum.Error;
            }


            int resultDbCode = 0;

            switch (resultEnum)
            {
            case SMSResultEnum.Delivered: resultDbCode = 1;
                break;

            case SMSResultEnum.Expired: resultDbCode = 3;
                break;

            case SMSResultEnum.NotDelivered: resultDbCode = 3;
                break;

            case SMSResultEnum.Unknown: resultDbCode = 4;
                break;

            case SMSResultEnum.Error: resultDbCode = 4;
                break;

            default: break;
            }

            MessageResultModel resultModel = new MessageResultModel
            {
                Id     = messageModel.Id,
                Status = resultDbCode
            };

            ConnectionFactory factory = new ConnectionFactory
            {
                UserName    = "******",
                Password    = "******",
                VirtualHost = "/",
                HostName    = "localhost"
            };

            string result = JsonSerializer.Serialize <MessageResultModel>(resultModel);

            var rabbitConnection = factory.CreateConnection();

            IModel model = rabbitConnection.CreateModel();

            model.ExchangeDeclare("messages_result", ExchangeType.Direct, true);

            byte[] messageBodyBytes = Encoding.UTF8.GetBytes(result);

            model.BasicPublish("messages_result", "messages_result", null, messageBodyBytes);
        }