MQTT中的内容类型替代 [英] Content-Type alternative in MQTT
问题描述
我正在使用Paho和Mosqitto进行MQTT通信.我们必须支持两种序列化模型-xml和json.因此,我正在寻找如何在MQTT中标识内容类型或有效负载类型. MQTT中是否有类似的HTTP Content-Type可以快速识别它?
I am Working on MQTT communication using Paho and Mosqitto. We have to support both model of serialization - xml and json. So I am looking How to identify the content type or payload type in MQTT. Is there something similar HTTP Content-Type in MQTT to identify it quickly ?
Content-Type:应用程序/json
Content-Type : application/json
Content-Type:应用程序/xml
Content-Type : application/xml
谢谢
推荐答案
不,MQTT负载只是字节数组,并且标头中没有空间(因为MQTT被设计为在网络上尽可能轻量).其他任何事情都取决于要在有效负载中实现的应用程序.
No, MQTT payloads are just byte arrays and there is no space in the headers (because MQTT is designed to be as light weight as possible on the network). Anything else is down to the application to implement with in the payload.
您可以使用多个主题来显示差异.
You could use multiple topics to show the difference.
例如 foo/bar/xml 或 foo/bar/json 并订阅 foo/bar/+ ,这两者都将匹配,然后根据主题.
e.g. foo/bar/xml or foo/bar/json and subscribe to foo/bar/+ which will match both and then switch based on the topic.
或仅测试有效负载的第一个字符'{'= json'<' = xml
or just test the first char of the payload, '{' = json '<' = xml
这篇关于MQTT中的内容类型替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!