The MQTT spec explicitly states that
MQTT规范明确规定
“sport/+” does not match “sport” but it does match “sport/”.
“sport/#” also matches the singular “sport”, since # includes the parent level.
But does “sport/#” also match “sport/”? The spec leaves this totally ambiguous.
但是“SPORT/#”也和“SPORT/”匹配吗?规范将这一点完全含糊不清。
As an aside, who else thinks allowing trailing slashes was a really bad design decision?
顺便说一句,还有谁认为允许尾随斜杠是一个非常糟糕的设计决定?
更多回答
The #
matches zero or more further elements so subscriptions to sport/#
will match sport/
#匹配零个或多个其他元素,因此对SPORT/#的订阅将与SPORT/匹配
This is easily tested with mosquitto_sub/mosquitto_pub
这很容易在蚊子/蚊子酒吧中进行测试
Publish:
发布:
$ mosquitto_pub -t "sport/" -m "foo"
Subscription:
订阅:
$ mosquitto_sub -v -t "sport/#"
sport/ foo
Yes, sport/#
matches sport/
. The confusing aspect of the spec is that unlike file and directory names, topic levels can be empty strings.
是的,体育/#比赛体育/。规范令人困惑的方面是,与文件名和目录名不同,主题级别可以是空字符串。
The spec says:
该规格说:
4.7.1.1 Topic level separator
The forward slash (‘/’ U+002F) is used to separate each level within a topic tree and provide a hierarchical structure to the Topic Names... Adjacent Topic level separators indicate a zero length topic level.
This means that sport/
is parsed as not one, but two topic levels -- sport
and the empty string -- hence the #
in sport/#
matches the second empty string topic level. This is the same reason that sport/+
matches sport/
-- the +
is matching the empty string level.
这意味着SPORT/被解析为两个主题级别--SPORT和空字符串--因此#in SPORT/#匹配第二个空字符串主题级别。这与SPORT/+匹配SPORT/的原因相同--+匹配空字符串级别。
更多回答
"zero or more further elements" is how I was basically thinking about it, but it also matches sport
, which is sort of like less than zero compared to sport/
, so I wish the spec would be clear about it
“零个或更多的其他元素”是我对它的基本想法,但它也与体育相匹配,与体育相比,体育是一种比零更少的元素,所以我希望规范能清楚地说明这一点
And if another library didn't match sport/
it would be debatable which is buggy without the spec being completely clear about it
如果另一个图书馆不符合SPORT/它将是有争议的,在没有完全明确的规范的情况下哪一个是错误的
so I wonder if there's any clarification in some other official documentation rather than observed behavior?
所以我想知道是否有任何澄清,在一些其他的官方文件,而不是观察到的行为?
Okay reading over other parts of the spec, I understand that sport/
actually has two "topic levels" -- sport
and the empty string, so in that case the #
in sport/#
is actually matching the empty string topic level, rather than zero topic levels.
好的,阅读规范的其他部分,我理解SPORT/实际上有两个“主题级别”--SPORT和空字符串,所以在这种情况下,#in SPORT/#实际上与空字符串主题级别匹配,而不是零主题级别。
我是一名优秀的程序员,十分优秀!