与 node.js 的 Live Objects MQTT 连接

Live Objects MQTT connection with node.js

我想为我的应用程序实现 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>
});