MQTT知道客户端是否已订阅 [英] MQTT know if a client is subscribed

查看:1940
本文介绍了MQTT知道客户端是否已订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已经发布,如何查找已连接的MQTT客户端详细信息

The question is already posted, Mqtt How a client can get to know that another client is connected or not and How to Find Connected MQTT Client Details

在我的情况下,如果客户端X已在频道A中订阅,则客户端Y不能订阅频道A,直到X取消订阅为止.我只能在该频道中订阅一个客户端

In my case, if client X is already subscribed in a channel A, client Y can't subcribe to the channel A, until X unsubcribes. I can only have one client subscribed in the channel

我还可以使用保留消息和LWT的想法吗?

Can I also use the idea of retained messages and LWT?

如果是,我不知道应该从哪里开始.最好从一个简单的示例开始,以查看保留的消息和LWT的工作方式.到目前为止,我仅具有发布和订阅方面的经验,但仅此而已.

If yes, I don't know exactly from where should I start. It would be good to start with a simple example to see how the retained messages and LWT work. So far, I just have experience in publishing and subscribing but no more.

能否请您告诉我一些建议,例如一些链接或示例或任何有用的信息,以便我作为一个起点.

Could you please, tell me some advises may be some links or examples or any useful information so I can have a starting point.

推荐答案

MQTT的目的是让多个客户订阅相同的主题,这是整个pub/sub模式和共享信息的一部分.因此,没有什么协议能够满足您的要求.

MQTT is all about having multiple clients subscribing to the same topics, it's part of the whole pub/sub pattern and sharing information. So there is nothing baked into the protocol that will do what you want.

您也许可以实现以下内容:

You may be able to implement something like the following:

如果主题为foo/bar,而您只希望一个订阅者,则可以将保留的消息以及该订阅者的客户ID的有效负载发布到lock/foo/bar.然后,您可以在断开连接时为该锁定主题发布免费"消息,并设置一个LWT以在客户端死亡时执行相同的操作.

If have a topic say foo/bar and you only want one subscriber you could publish a retained message with a payload of the client-id of the subscriber to lock/foo/bar. You could then publish a "free" to this lock topic when you disconnected and set up a LWT to do the same in case the client dies.

问题是所有东西都是异步的,因此为比赛条件打开了许多计时窗口.例如比如说client-1client-2都想订阅foo/bar,他们都需要先订阅lock/foo/bar来检查它的状态.他们俩几乎都在同一时间执行此操作,然后必须等待一段时间才能看到返回的消息(免费"或客户ID).他们都将获得免费",因此都将假定他们可以发布其客户ID. client-1首先发布,随后紧接着是client-2,然后他们都订阅了foo/bar.

The problem with this is that everything is asynchronous so it opens up lots of timing windows for race conditions. e.g. say client-1 and client-2 both want to subscribe to foo/bar, they would both need to first subscribe to lock/foo/bar to check it's state. They both do this at very nearly the same time, they then have to wait for some time to see what message they get back ("free" or a client-id). They would both get "free" so would both assume that they can publish their client-ids. client-1 published first shortly followed by client-2 and then they both subscribe to foo/bar.

这篇关于MQTT知道客户端是否已订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆