我有一个伊蚊MQTT 代理和我的 MQTT 客户端,但我似乎无法连接它们。
在我的app.js
我做以下:
(async function () {
try {
await startBroker();
await mqttClient();
} catch (e) {
console.error("ERROR: ", e);
process.exit();
}
})();
我的 startBroker 函数启动 aedes 并像这样流式传输它:
const aedes = require('aedes')();
const server = require('net').createServer(aedes.handle);
const port = 1883;
exports.startBroker = function() {
return new Promise((res, rej) => {
server.listen(port, function () {
console.log(`MQTT Broker started on port ${port}`);
return res()
});
})
};
然后我mqttClient
尝试连接,但是我永远无法真正连接。我已经针对工作正常的测试蚊子服务器对其进行了测试
const mqtt = require('mqtt');
const client = mqtt.connect("mqtt://localhost:1883");
exports.mqttClient = function() {
console.log("Connecting to MQTT Client");
client.on("connect", ack => {
console.log("MQTT Client Connected!");
client.on("message", (topic, message) => {
console.log(`MQTT Client Message. Topic: ${topic}. Message: ${message.toString()}`);
});
});
client.on("error", err => {
console.log(err);
});
}
有谁知道为什么我的经纪人似乎没有工作?