kamilfb / mqtt-spy

Please use the new Eclipse Paho GitHub repo instead.
https://github.com/eclipse/paho.mqtt-spy/wiki
346 stars 68 forks source link

Feature Request: Interfacing With Message Object - binary payloads #95

Closed ddeletic closed 8 years ago

ddeletic commented 8 years ago

Scripting: It would be great if there was a way of getting/setting message payloads with raw binary data rather than just strings. If the method already exists, it is not obvious.

kamilfb commented 8 years ago

Hi @ddeletic - I've updated the wiki with additional info. Is that what you were after? https://github.com/kamilfb/mqtt-spy/wiki/Scripting

ddeletic commented 8 years ago

Thanks for the quick response. It is great that the magic is already in there and I do not need to update my mqtt-spy.

One thing I'm not clear about is how to mqttspy.publish() binary data. All payloads are converted to strings in publish(), and I cannot publish a pre-built message object.

kamilfb commented 8 years ago

@ddeletic, what kind of binary data would you like to send? Have you got it already in something?

ddeletic commented 8 years ago
function getData(fName)
{
    var Paths = Java.type("java.nio.file.Paths");
    var Path  = Java.type("java.nio.file.Path");

    var path  = Paths.get(fName);
    var Files = Java.type("java.nio.file.Files");
    if (Files.exists(path) == true)
    {
        var data  = Files.readAllBytes(path);
        return data;
    }
    else
    {
        var data = new Int8Array(0);
        return data;
    }
}
. . . 
var payload = getData(FILE_NAME);
mqttspy.publish(topic, payload);

It is important to note that this is all done inside a scripted subscription

kamilfb commented 8 years ago

If you want to publish binary payload, you need to use this method signature:

mqtt.publish(topic, binaryPayload, qos, retained);

So you need to add QoS and the retained flag.

The Files.readAllBytes returns a byte array (byte[]).

Here is my example of publishing an image: https://github.com/kamilfb/mqtt-spy/blob/development/mqtt-spy/src/test/resources/scripts/publish_image.js

The publish method takes either a byte array (byte[]) or a string for the payload as long as you pass in the QoS and the retained flag.

ddeletic commented 8 years ago

Ok, I understand now. Thank you

kamilfb commented 8 years ago

No problem - if you need anything more, let me know.