如何查找已连接的MQTT客户端详细信息 [英] How to Find Connected MQTT Client Details

查看:1243
本文介绍了如何查找已连接的MQTT客户端详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是否可以通过其他任何方式找到另一个客户端的所有已连接客户端详细信息(IP和名称)?我知道有一个主题"$ SYS/broker/clients/active",该主题给出了当前连接的客户端的数量,但是如果我想了解有关每个连接的客户端的更多信息,有什么办法吗?

Is there any way we can find about all the connected client details(IP & name) from another client? I know there is a topic "$SYS/broker/clients/active" which gives the number of currently connected clients but if I want to know more about each connected client, is there any way?

我正在开发一种解决方案,其中许多客户端(使用无线网络)将连接到服务器上的MQTT代理.我还将在同一台计算机上运行另一个客户端,并将其连接到代理,该客户端将观察是否有任何新客户端与代理连接,或者是否有断开连接的客户端.当新客户端连接或连接的客户端断开连接时,我可以在代理控制台上看到消息.我们可以从连接到经纪人的客户那里得到类似的东西吗?请提出实现此目标的最佳方法是什么?

I'm developing a solution where number of client will be connected (using Wireless network) to MQTT broker located on a server. I will also have another client running on the same machine and connected to the broker which will observe if any new client connected with the broker or for a gets disconnected client. I can see message on broker console when a new client connects or connected client gets disconnected. Can we get something similar from a client connected to the broker? Please suggest what would be the best possible way to achieve this?

谢谢.

-Dilip

推荐答案

您的原始问题,也没有对后续问题的回答来标识您正在使用的代理实现.因此,对于您的问题可能会有更有效的答案.

Your original question, nor responses to subsequent questions identify which broker implementation you are using. So there may be a more efficient answer to your question.

在没有这些信息的情况下,让我们集中讨论可以在协议本身中进行的操作.

Without that information, let's focus on what you can do in the protocol itself.

MQTT支持RETAINED消息.代理将在此处存储针对每个主题的最新保留消息.客户端订阅该主题时,它将收到保留的消息(如果存在).

MQTT supports RETAINED messages. This is where the broker will store the most recent retained message against each topic. When a client subscribes to the topic, it will receive the retained message (if one exists).

还有goetzchr所指的最后遗嘱"(LWT)功能,如果它异常断开连接,可用于代表客户发布消息.

There is also the Last Will and Testament (LWT) feature (that goetzchr refers to), that can be used to publish a message on behalf of the client if it abnormally disconnects.

结合这两个功能,可以在协议内全部在代理上构建简单的状态服务.它是这样的:

Combining these two features allows you to build a simple presence service on the broker, all within the protocol. It works like this:

  1. 当客户端连接时,它会向其唯一的主题发布RETAINED消息,例如:

  1. when a client connects, it publishes a RETAINED message to a topic unique to it, for example:

clients/my_client_id/state

. (用客户自己的ID替换my_client_id.

with a payload of 1. (substituting my_client_id with the client's own id.

还在连接时将要发布的LWT消息设置为同一主题,但有效载荷为0.这也应该是RETAINED消息.

it also, on connect, sets a LWT message to be published to the same topic, but with a payload of 0. This should also be a RETAINED message.

当客户端干净地断开连接时,它会使用有效载荷0

when a client disconnects cleanly, it publishes a RETAINED message to the same topic with a payload of 0

这允许另一个客户端订阅clients/#,以接收所有指示客户端连接状态更改的消息(标识客户端的完整主题,以及指示连接状态的有效载荷值).

This allows another client to subscribe to clients/# to receive all the messages indicating the changes in clients' connection state (the full topic identifying the client, and the value of the payload indicating the connection state).

要获取不仅仅是连接状态的更多信息,客户端可以在连接时将另一个保留的消息发布到另一个主题,例如,clients/my_client_id/info,其中包含您感兴趣的所有信息.

To obtain more information than just connected state, the clients can publish another retained message on connect, to another topic, eg clients/my_client_id/info that contains all of the information you're interested in.

仅当您控制了连接到代理的所有客户端并能够使其表现出这种行为时,此选项才起作用.

This only works if you have control of all the clients that are connecting to your broker and able to get them to behave like this.

这不是理想的方法;希望您的代理实施会提供一些服务器端的方法.

This is not an ideal approach; hopefully your broker implementation will provide some server-side means of doing this.

这篇关于如何查找已连接的MQTT客户端详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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