0

我正在使用带有 EMQ X 代理的 mqtt 客户端(在我的本地 linux 机器中作为服务运行)并测试了 pub sub 机制,但我想使用代理事件功能(连接、断开、发布和订阅)来添加自定义代理事件的逻辑。请指导我如何在 emq X 经纪人中实现这一目标?

4

1 回答 1

0

使用 EMQ X REST API 构建自己的逻辑

https://github.com/wivwiv/egg-iot-with-mqtt

推荐使用 MQTT.js:

https://www.npmjs.com/package/mqtt#connect

const mqtt = require('mqtt')

// const url = 'mqtt://localhost:1883'
const url = 'mqtt://tools.emqx.io:1883'

const clinet = mqtt.connect(url, {
  clientId: 'emqx_client_id',
  username: '',
  password: '',
})

client.on('connect', () => {
  client.subscribe('presence', (err) => {
    if (!err) {
      setInterval(() => {
        client.publish('presence', 'Hello EMQ X')
      }, 1000)
    }
  })
})

client.on('message', (topic, message) => {
  // message is Buffer
  console.log('received from', topic, message.toString())
})

于 2019-09-26T07:02:47.720 回答