-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
29 lines (29 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var mosca = require('mosca');
require('dotenv').config();
var settings = {
port: 1883,
http: {
port: 8883
}
};
var server = new mosca.Server(settings); //สร้างตัวแปรมารับค่า server
server.on('ready', setup); //ใช้คำสั่ง ready,setup เพื่อตั้งค่า
function setup() {
server.authenticate = authenticate; // ตั้งให้เซิพเวอร์ต้องมี Authen
console.log('Mosca server is up and running (auth)')
}
var authenticate = function (client, username, password, callback) {
var authorized = (username === process.env.MQTT_USERNAME && password.toString() === process.env.MQTT_PASSWORD);
if (authorized) client.user = username;
callback(null, authorized);
}
server.on('clientConnected', function (client) {
console.log('Client Connected:', client.id);
});
server.on('clientDisconnected', function (client) {
console.log('Client Disconnected:', client.id);
});
server.on('published', function (packet, client) {
console.log(packet);
console.log('Published', packet.payload.toString());
});