我想为我的应用程序实现 Live Objects MQTT 接口,但我对这个协议不是很熟悉。有人有正确设置连接的代码示例吗?
谢谢你的回复!
您可以在 Github https://github.com/Orange-OpenSource/LiveObjects-samples-nodejs上找到一些 node.js 的代码示例
它也适用于其他平台,这里有 Github 上的链接:https ://developer.orange.com/apis/datavenue/code-sample
问候
这是一个示例:
const mqtt = require('mqtt');
const mqttTopic = "router/~event/v1/data/new/urn/lora/#";
const url = "mqtt://liveobjects.orange-business.com:1883";
const apiKey ="<your api key>";
let client = mqtt.connect(url, {
username: "payload",
password: apiKey,
keepAlive: 30
});
client.on("connect", function () {
console.log("Connected to Live Objects");
client.subscribe(mqttTopic);
console.log("MQTT::Subscribed to topic:", mqttTopic);
});
client.on("error", function (err) {
console.log("MQTT::Error from client --> ", err);
});
client.on("message", function (topic, message) {
let loraMessage = JSON.parse(message);
<your code here>
});