如何将nodejs上的mqtt客户端连接到在VM上运行的mqtt代理? 在此示例中,一旦客户端连接,它应该触发"已连接"记录,但它没有打印出来。
var mqtt = require("mqtt");
var http = require("http");
var express = require("express");
var app = express();
var client = mqtt.connect("mqtt://192.168.56.2:1883");
client.on("connect", function () {
client.subscribe("testTopic");
console.log("connected");
});
client.on("message", function (topic, message) {
// message is Buffer
console.log(message.toString());
//client.end();
});