在AMQP上使用MQTT消息

时间:2019-07-15 14:04:18

标签: c# rabbitmq mqtt amqp

我试图在网上寻找示例或解决方案,但是没有运气。基本上,我有RabbitMQ驻留在以这种方式发送消息的C#应用​​程序上。

    var factory = new ConnectionFactory() { HostName = "localhost", Port = 5672 };
    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
    {
        string message = "Hello World!";
        var body = Encoding.UTF8.GetBytes(message);

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

和一个简单的Javascript客户端,其中有 MQTT Paho js库(无nodejs),我在这里尝试以这种方式使用来自 AMQP 的消息:

var wsbroker = "localhost";  // mqtt websocket enabled broker
var wsport = 15675; // port for above
var client = new Paho.MQTT.Client(wsbroker, wsport, "/ws",
    "myclientid_" + parseInt(Math.random() * 100, 10));

    client.onConnect = function (responseObject){
        client.subscribe("TestMQTT");
    }
    client.onConnectionLost = function (responseObject) {
        debug("CONNECTION LOST - " + responseObject.errorMessage);
    };

    client.onMessageArrived = function (message) {
        debug("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString);
        print_first(message.payloadString);
    };

到目前为止,我什么也收不到,有人可以指出示例代码,向我指出正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您需要连接客户端吗?在上面的示例中,我没有看到.connect

client.connect({onSuccess:onConnect});