У меня есть aedes MQTT-брокер и мой MQTT-клиент, но я не могу их соединить.
В моем app.js
я делаю следующее:
(async function () {
try {
await startBroker();
await mqttClient();
} catch (e) {
console.error("ERROR: ", e);
process.exit();
}
})();
Моя функция startBroker запускает и выполняет потоковую передачу следующим образом:
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
пытается подключиться, однако я так и не смог установить соединение. Я проверил его на тестовом сервере Mosquitto, который работает нормально
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);
});
}
Кто-нибудь знает, почему мой брокер не работает?