forked from selaromdotnet/nativescript-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (88 loc) · 3.33 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var common_1 = require("./common");
var MQTT = require('./mqttws31');
var MQTTClient = (function () {
function MQTTClient(options) {
this.connectionSuccess = new common_1.EventHandler();
this.connectionFailure = new common_1.EventHandler();
this.connectionLost = new common_1.EventHandler();
this.messageArrived = new common_1.EventHandler();
this.connected = false;
this.host = options.host || 'localhost';
this.useSSL = options.useSSL || false;
if (options.port)
this.port = options.port;
else
this.port = this.useSSL ? 443 : 80;
this.path = options.path || '';
this.clientId = options.clientId || common_1.guid();
this.retryOnDisconnect = options.retryOnDisconnect || false;
this.mqttClient = new MQTT.Client(this.host, this.port, this.path, this.clientId);
this.mqttClient.useSSL = this.useSSL;
}
;
Object.defineProperty(MQTTClient.prototype, "onConnectionSuccess", {
get: function () { return this.connectionSuccess; },
enumerable: true,
configurable: true
});
Object.defineProperty(MQTTClient.prototype, "onConnectionFailure", {
get: function () { return this.connectionFailure; },
enumerable: true,
configurable: true
});
Object.defineProperty(MQTTClient.prototype, "onConnectionLost", {
get: function () { return this.connectionLost; },
enumerable: true,
configurable: true
});
Object.defineProperty(MQTTClient.prototype, "onMessageArrived", {
get: function () { return this.messageArrived; },
enumerable: true,
configurable: true
});
MQTTClient.prototype.connect = function (username, password) {
var _this = this;
if (this.connected) {
return;
}
var connectOptions = {
userName: username,
password: password,
useSSL: this.useSSL,
onSuccess: function () {
_this.connectionSuccess.trigger();
_this.connected = true;
},
onFailure: function (err) {
_this.connectionFailure.trigger(err.errorMessage);
}
};
this.mqttClient.onConnectionLost = function (err) {
_this.connectionLost.trigger(err.errorMessage);
_this.connected = false;
};
this.mqttClient.onMessageArrived = function (message) {
_this.messageArrived.trigger(new common_1.Message(message));
};
this.mqttClient.connect(connectOptions);
};
MQTTClient.prototype.subscribe = function (topic) {
this.mqttClient.subscribe(topic);
};
MQTTClient.prototype.unsubscribe = function (topic) {
this.mqttClient.unsubscribe(topic);
};
MQTTClient.prototype.publish = function (message) {
var mqttMessage = message.bytes !== null ?
new MQTT.Message(message.bytes) : new MQTT.Message(message.payload);
mqttMessage.destinationName = message.topic;
mqttMessage.retained = message.retained;
mqttMessage.qos = message.qos;
this.mqttClient.send(mqttMessage);
};
return MQTTClient;
}());
exports.MQTTClient = MQTTClient;
//# sourceMappingURL=index.js.map