I am developing a website which can connect to an mqtt broker and get payload messages.
The library I use is (https://eclipse.org/paho/clients/js/).
My problem is the following. When I try to get a standard compressed message (gzipped) the library throw an exception "Error: AMQJS0009E Malformed UTF data:f5 3 -52."
How can I handle compressed messages?
here is my code:
var selected_tags = '';
var checked_tags = [];
var hostname = 'xxx';
var port = 80;
var qos = 1;
var user = 'xxx';
var pass = 'xxx';
var keepAlive = 60;
var timeout = 3;
var ssl = false;
var cleanSession = true;
var lastWillTopic = '';
var lastWillQos = 1;
var lastWillRetain = false;
var lastWillMessage = '';
var g_topic;
var client = new Messaging.Client(hostname, port, "myclientid_" + parseInt(Math.random() * 100, 10));
var options = {
timeout : 3,
userName : user,
password : pass,
keepAliveInterval : keepAlive,
cleanSession : cleanSession,
useSSL : ssl,
onSuccess : function () {
console_log('<span style="color:green">Connected</span>');
},
onFailure : function (message) {
console_log('<span style="color:red">Connection failed: ' + message.errorMessage + '</span>');
client.connect(options);
}
};
function _subscribe(){
g_topic = $("#sel_reader").val();
console_log('<span style="color:green">subscribe to: ' + g_topic + '</span>');
client.subscribe(g_topic, {qos: qos});
$("#_subscribe").addClass('hide');
$("#_unsubscribe").removeClass('hide');
}
function _unsubscribe(){
console_log('<span style="color:red">unsubscribe from: ' + g_topic + '</span>');
client.unsubscribe(g_topic);
$("#_subscribe").removeClass('hide');
$("#_unsubscribe").addClass('hide');
}
client.onConnectionLost = function (responseObject) {
console_log('<span style="color:red">Connection lost: ' + responseObject.errorMessage + '</span>');
client.connect(options);
};
client.onMessageArrived = function (message) {
// console.log("message arrived");
var live_search = [];
var live_found = '';
var json = JSON.parse(message.payloadString);
console_log('<hr />Message Recieved: Topic: ' + message.destinationName + '<br />'
+message.payloadString +
'. QoS: ' + message.qos
);
};
var publish = function (payload, topic, qos) {
var message = new Messaging.Message(payload);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}
function console_log(txt){
$("#console").append('<b>'+txt + "</b><br />");
}
var prev_imei = '';
var find = ':';
var re = new RegExp(find, 'g');
function SortByRssi(a, b){
var arssi = a.rssi;
var brssi = b.rssi;
return ((arssi > brssi) ? -1 : ((arssi < brssi) ? 1 : 0));
}
$(document).ready(function(){
client.connect(options);
});