forked from snaptec/openWB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupMqttServices.js
56 lines (49 loc) · 1.54 KB
/
setupMqttServices.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
/**
* Functions to provide services for MQTT
* topic set with array var topicsToSubscribe has to be loaded before
*
* @author Kevin Wieland
* @author Michael Ortenstein
*/
//Connect Options
var isSSL = location.protocol == 'https:'
var options = {
timeout: 5,
useSSL: isSSL,
//Gets Called if the connection has been established
onSuccess: function () {
topicsToSubscribe.forEach((topic) => {
client.subscribe(topic[0], {qos: 0});
});
},
//Gets Called if the connection could not be established
onFailure: function (message) {
client.connect(options);
}
};
var clientuid = Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, 5);
var client = new Messaging.Client(location.hostname, 9001, clientuid);
$(document).ready(function(){
client.connect(options);
});
//Gets called if the websocket/mqtt connection gets disconnected for any reason
client.onConnectionLost = function (responseObject) {
client.connect(options);
};
//Gets called whenever you receive a message
client.onMessageArrived = function (message) {
// func processMessages defined in respective processAllMqttMsg_
processMessages(message.destinationName, message.payloadString);
};
client.onDeliveryComplete = function (token) {
// func processMessages defined in respective processAllMqttMsg_
console.log("token");
};
//Creates a new Messaging.Message Object and sends it
function publish(payload, topic) {
var message = new Messaging.Message(payload);
message.destinationName = topic;
message.qos = 2;
message.retained = true;
client.send(message);
}