我有一个nodejs(node v0.10.26)MQTT代码,它将字符串发布到远程MQTT服务器上的主题。现在我试图将一个字节数组和一个缓冲区发布到同一主题,但是我收到了一个错误 代码:
var ProtoBuf = require("protobufjs");
var mqtt = require('mqtt');
var builder = ProtoBuf.loadProtoFile('event.proto'),
as2 = builder.build('as2'),
Message=as2.Message;
var message = new Message({
"message_type": "EMOTICON"
});
console.log("message : "+message);
var buffer_message= message.encode();
client = mqtt.createClient(1883,'hostNameOfMQTT',{ encoding: 'binary' });
client.publish('NewTopic',buffer_message);
client.on('connect', function() {
console.log("Message published : "+buffer_message);
client.end();
});
我执行时遇到此错误 node.exe sampleMqtt.js
C:\ node> node.exe SimpleMQTT.js 消息:.as2.Message
C:\节点\ node_modules \ MQTT \ lib中\ generate.js:178 length + = Buffer.byteLength(payload);
^
TypeError: Argument must be a string
at Object.module.exports.publish (C:\node\node_modules\mqtt\lib\
generate.js:178:22)
at Connection.eval [as publish] (eval at <anonymous> (C:\node\no
de_modules\mqtt\lib\connection.js:58:29), <anonymous>:2:26)
at MqttClient._sendPacket (C:\node\node_modules\mqtt\lib\client.
js:430:20)
at MqttClient.<anonymous> (C:\node\node_modules\mqtt\lib\client.
js:105:12)
at MqttClient.EventEmitter.emit (events.js:117:20)
at MqttClient._handleConnack (C:\node\node_modules\mqtt\lib\clie
nt.js:498:10)
at Connection.<anonymous> (C:\node\node_modules\mqtt\lib\client.
js:191:10)
at Connection.EventEmitter.emit (events.js:95:17)
at Connection._write (C:\node\node_modules\mqtt\lib\connection.j
s:187:12)
at doWrite (_stream_writable.js:226:10)
但是当我尝试发布buffer_message.tostring()时,我将对象详细信息作为字符串而不是实际的ArrayBuffer? client.publish(&#39; AnkitTopic&#39;,buffer_message.toString()); 用toString()输出 - &gt;字节缓冲区(offser = 0,markedOffSet = -1,长度= 2,容量= 16)
我不想要字符串,我希望实际的byteBuffer通过MQTT传输。
有人,请建议我该怎么办!!我猜它不是很难得到,它在java中是可能的,那么为什么不在nodeJS中?
答案 0 :(得分:0)
您是否可以尝试发送其他类型的二进制内容(如文件)以确定其有效?
最新的MQTT.js版本支持二进制有效负载https://github.com/adamvr/MQTT.js/issues/109
答案 1 :(得分:0)
我找到了这个问题的解决方案,并希望它能帮助其他人:
我们可以为每个发送到MQ的有效负载附加一个头,对其进行编码,最终的有效负载应该看起来类似(这就是我们将它转换为字节缓冲区的方式):
var headerLengthBuffer = new ByteBuffer();
var headerBuffer = header.encode();
var messageLengthBuffer = new ByteBuffer();
var messageBuffer = event1.encode();
headerLengthBuffer.writeVarint32(headerBuffer.length);
messageLengthBuffer.writeVarint32(messageBuffer.length);
//Create the payload object
var payload = headerLengthBuffer.toBuffer() + headerBuffer.toBuffer()+ messageLengthBuffer.toBuffer() + messageBuffer.toBuffer() ;
return payload;
创建的上述有效负载可以轻松发布,也可以被MQ接受。
答案 2 :(得分:0)
根据protobufjs/message API,你只需要调用message.toBuffer()。
这是一个有效的例子:
var ProtoBuf = require("protobufjs");
var mqtt = require('mqtt');
var builder = ProtoBuf.loadProtoFile('company.proto'),
Company = builder.build('Company'),
Employee = Company.Employee;
var employee = new Employee("James Bond", 007);
var buffer_message = employee.toBuffer();
var client = mqtt.connect('mqtt://test.mosquitto.org');
client.on('connect', function() {
client.publish('MyCompany', buffer_message);
client.end();
});
使用以下 company.proto 文件:
package Company;
message Employee{
optional string name = 1;
optional uint32 id = 2;
}