Mqtt精髓系列之连接建立过程

来源:互联网 发布:达梦数据库下载网盘 编辑:程序博客网 时间:2024/06/07 03:17

翻译:https://www.hivemq.com/blog/mqtt-essentials-part-3-client-broker-connection-establishment

连接过程

  首先,客户端发送CONNECT消息请求建立连接;其次,broker返回CONNACK消息,如下图所示:
这里写图片描述

通过NAT连接

It is a common use case that MQTT clients are behind routers, which are using network address translation (NAT) in order to translate from a private network address (like 192.168.x.x, 10.0.x.x) to a public facing one. As already mentioned the MQTT client is doing the first step by sending a CONNECT message. So there is no problem at all with clients behind a NAT, because the broker has a public address and the connection will be kept open to allow sending and receiving message bidirectional after the initial CONNECT.

CONNECT关键字段

CONNECT消息中常用关键字段如下图所示:
这里写图片描述

Clean Session

功能:指示Broker客户端是否想要建立持久会话(CleanSession为false)。持久会话意味着Broker需要存储客户端所有的订阅关系,以及断连之后Qos1和Qos2的消息。非持久会话意味着每次建立连接后创建新的会话,会话仅持续与连接相同的时间。

The clean session flag indicates the broker, whether the client wants to establish a persistent session or not. A persistent session (CleanSession is false) means, that the broker will store all subscriptions for the client and also all missed messages, when subscribing with Quality of Service (QoS) 1 or 2. If clean session is set to true, the broker won’t store anything for the client and will also purge all information from a previous persistent session.

Will Message

功能:客户端连接异常断开时,由Broker将其遗嘱消息发送给订阅方。客户端与Broker建立连接时将遗嘱消息包含在CONNECT消息中发送给Broker。

The will message is part of the last will and testament feature of MQTT. It allows to notify other clients, when a client disconnects ungracefully. A connecting client will provide his will in form of an MQTT message and topic in the CONNECT message. If this clients gets disconnected ungracefully, the broker sends this message on behalf of the client.

Username/Password

  建立连接时,CONNECT消息中可以携带用户名和密码用于身份校验,但是默认是以明文方式传输,推荐使用TLS方式或者将密码加密后再进行传输。

MQTT allows to send a username and password for authenticating the client and also authorization. However, the password is sent in plaintext, if it isn’t encrypted or hashed by implementation or TLS is used underneath. We highly recommend to use username and password together with a secure transport of it. In brokers like HiveMQ it is also possible to authenticate clients with an SSL certificate, so no username and password is needed.

KeepAlive

The keep alive is a time interval, the clients commits to by sending regular PING Request messages to the broker. The broker response with PING Response and this mechanism will allow both sides to determine if the other one is still alive and reachable.

CONNACK关键字段

Session Present flag

功能: Broker是否有当前客户端的持久会话。

The session present flag indicate, whether the broker already has a persistent session of the client from previous interactions. If a client connects and has set CleanSession to true, this flag is always false, because there is no session available. If the client has set CleanSession to false, the flag is depending on, if there are session information available for the ClientId. If stored session information exist, then the flag is true and otherwise it is false. This flag was added newly in MQTT 3.1.1 and helps the client to determine, whether it has to subscribe to topics or if these are still stored in his session.

Connect acknowledge flag

功能: 指示连接是否成功;

原创粉丝点击