gpt4 book ai didi

jquery - Paho javascript mqtt 压缩有效负载

转载 作者:行者123 更新时间:2023-12-01 07:45:05 28 4
gpt4 key购买 nike

我正在开发一个可以连接到 mqtt 代理并获取有效负载消息的网站。

我使用的库是( https://eclipse.org/paho/clients/js/ )。

我的问题如下。当我尝试获取标准压缩消息(gzipped)时,库抛出异常“错误:AMQJS0009E 格式错误的 UTF 数据:f5 3 -52。”

如何处理压缩消息?

这是我的代码:

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);
});

最佳答案

如果有人需要 Python 的答案:

import zlib

def _on_message(self, mqttc, obj, msg):
dec_msg = zlib.decompress(msg.payload)
# do whatever you need with dec_msg
print dec_msg

关于jquery - Paho javascript mqtt 压缩有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39771422/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com