Content-Type alternative in MQTT

痴心易碎 提交于 2019-12-23 01:28:40

问题


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 : application/json

Content-Type : application/xml

Thanks


回答1:


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.

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.

or just test the first char of the payload, '{' = json '<' = xml



来源:https://stackoverflow.com/questions/31988877/content-type-alternative-in-mqtt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!