Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
});
server.on("listening", function () {
var addr = server.address();
logger.info('Starting UDP Server on %s:%d', addr.address, addr.port);
});
server.bind(SERVER_UDP_PORT);
// ************************************************************
// MQTT Server
// ************************************************************
logger.info('Starting MQTT broker on %s ...', SERVER_MQTT_PORT);
mqtt.createServer(function(client) {
logger.info('Server created...');
client.on('connect', function(packet) {
client.connack({returnCode: 0});
client.id = packet.clientId;
logger.info('MQTT Client connected: %s', packet.clientId);
});
client.on('publish', function(packet) {
logger.info('MQTT Topic: %s Payload: %s', packet.topic, packet.payload);
makeMetrics(packet.topic, JSON.parse(packet.payload));
});
client.on('pingreq', function(packet) {
client.pingresp();
if (fs.existsSync(verifyCertKeyPath)) {
tlsArgs = {
keyPath: conf.connector.mqtt.key || './certs/client.key',
certPath: conf.connector.mqtt.crt || './certs/client.crt',
keepalive: 59000
};
} else {
// load from /usr/share
tlsArgs = {
keyPath: '/usr/share/iotkit-agent/certs/enableiot_agent.key',
certPath: '/usr/share/iotkit-agent/certs/enableiot_agent.crt',
keepalive: 59000
};
}
var mqttServer = mqtt.createServer(function(client) {
client.on('connect', function(packet) {
client.connack({returnCode: 0});
client.id = packet.clientId;
logger.debug('MQTT Client connected: ', packet.clientId);
});
client.on('publish', function(packet) {
logger.debug('MQTT Topic: %s Payload: %s', packet.topic, packet.payload);
try {
onMessage(JSON.parse(packet.payload));
} catch (ex) {
logger.error('MQTT Error on message: %s', ex);
}
});
it("spec name", function () {
var mqttServer = mqtt.createServer(mqttModule).listen(1884, function () {
console.log("mqtt server listening on port %d", 1884);
});
mqttServer.close();
});
});
module.exports = function (port) {
return mqtt.createServer(function (client) {
var self = this;
if (!self.clients) self.clients = {};
client.on('connect', function (packet) {
self.clients[packet.client] = client;
client.id = packet.client;
client.subscriptions = [];
client.connack({returnCode: 0});
});
client.on('subscribe', function (packet) {
var granted = [];
for (var i = 0; i < packet.subscriptions.length; i++) {
var qos = packet.subscriptions[i].qos
var config = require('./config')
, mqtt = require('mqtt')
, nitrogen = require('nitrogen');
//TODO: need to move config to file
var AzureEventHubManager = require("./AzureEventHubManager.js")
var aehm = new AzureEventHubManager(config.eventhub_namespace, config.eventhub_hubname ,config.eventhub_keyname, config.eventhub_keyvalue)
var service = new nitrogen.Service(config);
var FAILURE = 1;
var SUCCESS = 0;
var mqttServer = mqtt.createServer(function(client) {
client.on('connect', function(packet) {
console.log("CONNECT: " + JSON.stringify(packet));
if (!packet.username || !packet.password) {
console.log("Error: No username or password.");
return client.connack({ returnCode: 1 });
}
var principal = new nitrogen.Device({
accessToken: {
token: packet.password
},
id: packet.username,
nickname: packet.username
});
Connector.prototype.start = function(cb) {
var self = this;
this.mqttServer = mqtt.createServer();
this.mqttServer.on('client', function(client) {
client.on('error', function(err) {
client.stream.destroy();
});
client.on('close', function() {
client.stream.destroy();
});
client.on('disconnect', function(packet) {
client.stream.destroy();
});
if(self.opts.disconnectOnTimeout) {
var timeout = self.opts.timeout * 1000 || constants.TIME.DEFAULT_MQTT_HEARTBEAT_TIMEOUT;
client.stream.setTimeout(timeout,function() {
var create_server = function () {
mqtt
.createServer(server_client)
.listen(settings.d.mqttd.port, settings.d.mqttd.host);
logger.info({
method: "create_server",
port: settings.d.mqttd.port,
host: settings.d.mqttd.host,
}, "listening for MQTT");
};