mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-09-07 01:41:57 +00:00
![Matthew Nickson](/assets/img/avatar_default.png)
* Added JSDoc to eslint rules Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com> * Fixed JSDoc eslint errors Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com> * Update the check-linters workflow to Node.js 20 --------- Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com> Co-authored-by: Louis Lam <louislam@users.noreply.github.com>
58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
const { log } = require("../src/util");
|
|
|
|
const mqttUsername = "louis1";
|
|
const mqttPassword = "!@#$LLam";
|
|
|
|
class SimpleMqttServer {
|
|
aedes = require("aedes")();
|
|
server = require("net").createServer(this.aedes.handle);
|
|
|
|
/**
|
|
* @param {number} port Port to listen on
|
|
*/
|
|
constructor(port) {
|
|
this.port = port;
|
|
}
|
|
|
|
/**
|
|
* Start the MQTT server
|
|
* @returns {void}
|
|
*/
|
|
start() {
|
|
this.server.listen(this.port, () => {
|
|
console.log("server started and listening on port ", this.port);
|
|
});
|
|
}
|
|
}
|
|
|
|
let server1 = new SimpleMqttServer(10000);
|
|
|
|
server1.aedes.authenticate = function (client, username, password, callback) {
|
|
if (username && password) {
|
|
console.log(password.toString("utf-8"));
|
|
callback(null, username === mqttUsername && password.toString("utf-8") === mqttPassword);
|
|
} else {
|
|
callback(null, false);
|
|
}
|
|
};
|
|
|
|
server1.aedes.on("subscribe", (subscriptions, client) => {
|
|
console.log(subscriptions);
|
|
|
|
for (let s of subscriptions) {
|
|
if (s.topic === "test") {
|
|
server1.aedes.publish({
|
|
topic: "test",
|
|
payload: Buffer.from("ok"),
|
|
}, (error) => {
|
|
if (error) {
|
|
log.error("mqtt_server", error);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
});
|
|
|
|
server1.start();
|