mqttOutMsg.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. --- 模块功能:MQTT客户端数据发送处理
  2. module(...,package.seeall)
  3. --数据发送的消息队列
  4. local msgQueue = {}
  5. local function insertMsg(topic,payload,qos,user)
  6. table.insert(msgQueue,{t=topic,p=payload,q=qos,user=user})
  7. sys.publish("APP_SOCKET_SEND_DATA")
  8. end
  9. --发布回调函数
  10. local function pubQos1TestCb(result)
  11. log.info("mqttOutMsg.pubQos1TestCb",result)
  12. end
  13. --订阅mqtt_send消息,当录音时执行,将录音流数据上传
  14. sys.subscribe("mqtt_send", function(data)
  15. insertMsg("mqtt/full/1",data,0,{cb=pubQos1TestCb})
  16. end)
  17. --- MQTT客户端数据发送处理
  18. -- @param mqttClient,MQTT客户端对象
  19. -- @return 处理成功返回true,处理出错返回false
  20. -- @usage mqttOutMsg.proc(mqttClient)
  21. function proc(mqttClient)
  22. while #msgQueue>0 do
  23. local outMsg = table.remove(msgQueue,1)
  24. local result = mqttClient:publish(outMsg.t,outMsg.p,outMsg.q)
  25. if outMsg.user and outMsg.user.cb then outMsg.user.cb(result,outMsg.user.para) end
  26. if not result then return end
  27. end
  28. return true
  29. end