如何使用RabbitMQ将消息从模拟MQTT设备发送到SQL数据库?

时间:2017-06-12 07:10:52

标签: rabbitmq iot

我正在关注教程 - https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

它为设备提供代码

using System;
using RabbitMQ.Client;
using System.Text;

class Send
{
     public static void Main()
    {    
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            string message = "Hello World!";
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "",
                                 routingKey: "hello",
                                 basicProperties: null,
                                 body: body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
        }
    }

接收者为

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

class Receive
 {
     public static void Main()
     {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: "hello",
                                 noAck: true,
                                 consumer: consumer);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }  
    }
}

这将消息从我的电脑上的一个终端发送到另一个终端。 但我想将消息发送并存储到我的SQL服务器。我必须为此做些什么。 附:我不是一个开发人员,所以你必须详细说明你的答案

0 个答案:

没有答案