Skip to content

Commit

Permalink
Trying to understand the code and already adding scheme to applicatio…
Browse files Browse the repository at this point in the history
…nserver for mqtts:// support, defaults to mqtt://
  • Loading branch information
StWiemann committed Jan 22, 2020
1 parent 22f66d9 commit 6bb8214
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/applicationServers/loraserverioAppService.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class LoraserverIoService extends appService.AbstractAppService {
this.applicationServer.host,
this.applicationServer.username,
this.applicationServer.password,
this.applicationServer.scheme,
this.preProcessMessage
);

Expand Down
12 changes: 7 additions & 5 deletions lib/bindings/mqttClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class MqttClient {
* @param {<type>} username The username
* @param {<type>} password The password
* @param {<type>} listener The listener
* @param {<type>} scheme The scheme
*/
constructor(host, username, password, listener) {
constructor(host, username, password, listener, scheme = 'mqtt://') {
if (!host || !listener) {
throw new Error('Invalid arguments');
}
Expand All @@ -46,6 +47,7 @@ class MqttClient {
this.password = password;
this.topics = {};
this.listener = listener;
this.scheme = scheme;
}

/**
Expand All @@ -54,7 +56,7 @@ class MqttClient {
* @param {Function} callback The callback
*/
start(callback) {
var host = 'mqtt://' + this.host;
var host = this.scheme + this.host;
var options = {};
options.username = this.username;
options.password = this.password;
Expand All @@ -65,11 +67,11 @@ class MqttClient {
host,
options
);
this.mqttClient.on('error', function(error) {
this.mqttClient.on('error', function (error) {
winston.error('Error connecting to MQTT server:' + JSON.stringify(error));
});
this.mqttClient.on('message', this.listener);
this.mqttClient.on('connect', function(object, binding) {
this.mqttClient.on('connect', function (object, binding) {
winston.info('Connected to MQTT server');
if (!connected) {
connected = true;
Expand All @@ -84,7 +86,7 @@ class MqttClient {
* @param {Function} callback The callback
*/
stop(callback) {
this.mqttClient.end(function() {
this.mqttClient.end(function () {
if (callback) {
return callback();
}
Expand Down

0 comments on commit 6bb8214

Please sign in to comment.