Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var moscaOpts = {
port: 6884,
stats: false,
logger: {
level: "fatal"
}
};
var clientOpts = {
json: false,
mqtt: require("mqtt"),
host: "127.0.0.1",
port: 6884
};
var mqttTestServer = new mosca.Server(moscaOpts);
var newClient = new ascoltatori.MQTTAscoltatore(clientOpts);
steed.series([
function(cb) {
newClient.once('ready',cb);
},
function(cb) {
// Subscribe to topic for test
newClient.subscribe('reconnect/test', function() {
newClient.emit('success');
}, cb);
},
// Stop the MQTT server
function(cb) {
var username = "test";
var password = "pass";
var settings = {
id: 'broker',
stats: true,
port: 1883,
persistence: { factory: mosca.persistence.Memory },
http: {
port: 8080,
bundle: true,
static: './'
}
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('CON %s', client.id);
if (autoKickOff && client.id != "AdamHorcica_161959140") {
setTimeout(() => {
console.log("KICK client %s", client.id);
client.close();
}, 1000 + Math.random() * 5000);
}
});
// fired when a message is received
server.on('published', function(packet, client) {
//console.log('PUB %s "%s"', packet.topic, packet.payload);
});
module.exports = function(debug) {
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
if (debug !== undefined)
console.log('Client connected: ', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
if (debug !== undefined)
console.log('Message published: ', packet.payload);
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
// var settings = {
// port: 3001,
// logger: {
// name: "secureExample",
// level: 40,
// }/*,
// secure : {
// port: 8443,
// keyPath: SECURE_KEY,
// certPath: SECURE_CERT,
// }*/
// };
var server = new mosca.Server(settings);
server.on('ready', setup);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
server.on('published', function(packet, client) {
console.log('Published', packet.topic, packet.payload);
});
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'ascoltatori',
mongo: {}
};
const settings = {
port: 1883,
backend: ascoltatore,
http: {
port: 3000,
bundle: true,
static: './'
}
};
const server = new mosca.Server(settings);
server.on('clientConnected', (client) => {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', (packet, client) => {
console.log('Server published. Avoid showing package content here.');
});
// fired when the mqtt server is ready
server.on('ready', () => {
console.log('Mosca server is up and running');
});
type: 'mongo',
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'ascoltatori',
mongo: {}
};
var moscaSettings = {
port: 1883,
backend: ascoltatore,
persistence: {
factory: mosca.persistence.Mongo,
url: 'mongodb://localhost:27017/mqtt'
}
};
var server = new mosca.Server(moscaSettings);
server.on('ready', setup);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', packet.payload);
});
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running')
}
type: 'mqtt',
mqtt: require('mqtt'),
json: false,
port: 1883,
http: {
port: 3000,
bundle: true,
static: './'
}
};
var regular_topics = ['test/topic', 'test', 'iot', 'mosquitto', 'all']; // TOPICS
// ... prendere i regular_topics da auth.credentials (collegato direttamente a credentials.json)
var server = new mosca.Server(settings);
var db = new mosca.persistence.LevelUp({ path: './db_levelup' }); // DB storing retained messages
db.wire(server);
var auth_required = auth.credentials.auth_required; // se true necessario LOGGARSI
// fired when a client connects
server.on('clientConnected', function(client) {
console.log('- Client connected: ', client.id);
});
// fired when a client disconnects
server.on('clientDisconnected', function(client) {
console.log('- Client Disconnected: ', client.id);
});
},
(req, res, next) => {
if (req.user) { // 如识别到JWT中的 Payload 则查库进行二次验证
userModel.findById(req.user.id).then(doc => {
if (!doc) { return res.status(404).json({ msg: 'User is not found!' }) } // HTTP 401 token对应用户不存在
if (doc.role !== req.user.role) { return res.status(401).json({ msg: 'Token is outdated!' }) } // HTTP 401 token已过期(权限更改)
return next()
})
.catch(err => res.status(500).json({ msg: err.message, error: err }))
} else {
return next()
}
})
// Run
const server = new mosca.Server(config.mqtt) // here we start mosca
const msg = new MsgServer(server, io)
msg.setup()
app.use((req, res, next) => {
req.mqtt = server
req.io = io
next()
})
app.use('/', routes)
httpserver.listen(config.server.port, () => {
console.log(`Magic happens on port ${config.server.port}`)
})
io.on('connection', socket => {})
export const createMqttServer = () => {
const server = new mosca.Server({
http: {
port: 3000,
bundle: true,
static: './',
},
});
return server;
};
return new Promise(function(resolve, reject) {
mqttServer = new mosca.Server(moscaSettings, function() {
resolve();
});
});
});