cloudampq客户端未连接到Rabbitmq服务器

时间:2018-08-20 17:29:33

标签: cloudamqp

我的代码如下所示:

var mqtt = require('mqtt'), url = require('url');
var url = "mqtt://cloudamqp_username:cloudamqp_password@hostname:port";

var options = {
    port: 1883,
    clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
    username: 'cloudamqp_username',
    password: 'cloudamqp_password'
};


var client = mqtt.connect(url,options);

client.on('connect', function () { // When connected

    // subscribe to a topic
    client.subscribe('hello/world', function () {
        // when a message arrives, do something with it
        client.on('message', function (topic, message, packet) {
            console.log("Received '" + message + "' on '" + topic + "'");
        });
    });

    // publish a message to a topic
    client.publish('hello/world', 'my message', function () {
        console.log("Message is published");
        client.end(); // Close the connection when published
    });
});

0 个答案:

没有答案