如何从 MQTT 主题中提取数值

How to extract a numeric value from MQTT topic

我是 MQTT 新手。我的 mqtt 主题如下 hello/receiver/{bookId}/{stId}

book id 和 stid 可以是客户端发布的任何整数。我的目标是订阅这个主题并获取 bookid 和 stid 的值。我的问题是如何订阅包含某些值的主题,这些值可以在每次发布消息时更改。

MQTT 支持 2 种类型的通配符,订阅时可以在主题模式中使用。

  • + 匹配单个级别
  • # 可以匹配多个级别(但只能在主题末尾)

例如hello/receive/+/+ 将匹配您要查找的内容

hello/receive/# 也会匹配,但还会包括 hello/receivehello/receive/foohello/receive/foo/barhello/receive/foo/bar/baz 等等。