Class ConnectPacket
The first packet sent by the client to the server after establishing the network connection. A client MUST send a CONNECT packet as its first packet, and the server MUST treat any other packet received before CONNECT as a protocol violation.
The variable header contains the protocol name, protocol level (version), connect flags, and keep-alive interval. The payload contains, in order: the Client Identifier, Will Topic, Will Message, User Name, and Password — each present only if the corresponding connect flag is set.
Connect Flags (byte 8 of variable header)
| Bit 7 | User Name Flag |
| Bit 6 | Password Flag |
| Bit 5 | Will Retain |
| Bits 4–3 | Will QoS |
| Bit 2 | Will Flag |
| Bit 1 | Clean Session |
| Bit 0 | Reserved (must be 0) |
- Author:
- Chris Burdess
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the Client Identifier.intReturns the Keep Alive interval in seconds.byte[]Returns the Password.Returns the MQTT 5.0 properties for this packet.Returns the User Name.Returns the MQTT protocol version indicated by the Protocol Level field in the variable header.byte[]Returns the Will Message payload.Returns the Will Properties (MQTT 5.0 only).Returns the Will QoS level (bits 4–3 of the Connect Flags).Returns the Will Topic.booleanReturns the Clean Session flag (bit 1 of the Connect Flags).booleanReturns the Will Flag (bit 2 of the Connect Flags).booleanReturns the Will Retain flag (bit 5 of the Connect Flags).voidsetCleanSession(boolean cleanSession) Sets the Clean Session flag.voidsetClientId(String clientId) Sets the Client Identifier.voidsetKeepAlive(int keepAlive) Sets the Keep Alive interval in seconds.voidsetPassword(byte[] password) Sets the Password.voidsetProperties(MQTTProperties properties) Sets the MQTT 5.0 properties for this packet.voidsetUsername(String username) Sets the User Name.voidsetVersion(MQTTVersion version) Sets the MQTT protocol version.voidsetWillFlag(boolean willFlag) Sets the Will Flag.voidsetWillPayload(byte[] willPayload) Sets the Will Message payload.voidsetWillProperties(MQTTProperties willProperties) Sets the Will Properties (MQTT 5.0 only).voidsetWillQoS(QoS willQoS) Sets the Will QoS level.voidsetWillRetain(boolean willRetain) Sets the Will Retain flag.voidsetWillTopic(String willTopic) Sets the Will Topic.toString()
-
Constructor Details
-
ConnectPacket
public ConnectPacket()Creates a new CONNECT packet with default values.
-
-
Method Details
-
getProperties
Returns the MQTT 5.0 properties for this packet.- Returns:
- the properties, never null
- See Also:
-
setProperties
Sets the MQTT 5.0 properties for this packet.- Parameters:
properties- the properties, or null for empty
-
getVersion
Returns the MQTT protocol version indicated by the Protocol Level field in the variable header.- Returns:
- the protocol version
- See Also:
-
setVersion
Sets the MQTT protocol version.- Parameters:
version- the protocol version- See Also:
-
isCleanSession
public boolean isCleanSession()Returns the Clean Session flag (bit 1 of the Connect Flags).If true, the client and server MUST discard any previous session state and start a new session. If false, the server MUST resume communication based on the stored session state (subscriptions and pending QoS 1/2 messages).
In MQTT 5.0 this is renamed to Clean Start.
- Returns:
- true if a clean session is requested
- See Also:
-
setCleanSession
public void setCleanSession(boolean cleanSession) Sets the Clean Session flag.- Parameters:
cleanSession- true for a clean (new) session- See Also:
-
getKeepAlive
public int getKeepAlive()Returns the Keep Alive interval in seconds.This is the maximum time interval between control packets sent by the client. If no other packets are sent within this period, the client MUST send a PINGREQ. The server MUST disconnect a client that has not sent a packet within 1.5 times the Keep Alive value.
A value of 0 means keep-alive is disabled.
- Returns:
- the keep-alive interval in seconds
- See Also:
-
setKeepAlive
public void setKeepAlive(int keepAlive) Sets the Keep Alive interval in seconds.- Parameters:
keepAlive- the keep-alive interval (0 to disable)- See Also:
-
getClientId
Returns the Client Identifier.The Client Identifier uniquely identifies the client to the server and MUST be present in every CONNECT packet. The server uses this to identify session state. In MQTT 3.1.1, it MUST be between 1 and 23 UTF-8 encoded bytes of characters
[0-9a-zA-Z], though servers MAY allow longer or richer identifiers.An empty Client Identifier is valid only if Clean Session is true; the server then assigns a unique identifier.
- Returns:
- the client identifier, or null/empty if to be assigned
- See Also:
-
setClientId
Sets the Client Identifier.- Parameters:
clientId- the client identifier- See Also:
-
isWillFlag
public boolean isWillFlag()Returns the Will Flag (bit 2 of the Connect Flags).If true, a Will Message MUST be stored on the server and published to the Will Topic when the connection is closed unexpectedly (without a DISCONNECT packet). The Will Topic and Will Message fields in the payload MUST be present when this flag is set.
- Returns:
- true if a Will Message is specified
- See Also:
-
setWillFlag
public void setWillFlag(boolean willFlag) Sets the Will Flag.- Parameters:
willFlag- true to include a Will Message- See Also:
-
getWillQoS
Returns the Will QoS level (bits 4–3 of the Connect Flags).Specifies the QoS level to use when publishing the Will Message. Only meaningful when
isWillFlag()is true; if the Will Flag is false, this MUST be 0.- Returns:
- the Will QoS level, or null if not set
- See Also:
-
setWillQoS
Sets the Will QoS level.- Parameters:
willQoS- the QoS level for the Will Message- See Also:
-
isWillRetain
public boolean isWillRetain()Returns the Will Retain flag (bit 5 of the Connect Flags).If true, the Will Message MUST be published as a retained message when the will is triggered. Only meaningful when
isWillFlag()is true.- Returns:
- true if the Will Message should be retained
- See Also:
-
setWillRetain
public void setWillRetain(boolean willRetain) Sets the Will Retain flag.- Parameters:
willRetain- true to retain the Will Message- See Also:
-
getWillTopic
Returns the Will Topic.The topic to which the Will Message will be published if the client disconnects ungracefully. Present in the payload only when
isWillFlag()is true.- Returns:
- the Will Topic, or null if no will is set
- See Also:
-
setWillTopic
Sets the Will Topic.- Parameters:
willTopic- the topic for the Will Message- See Also:
-
getWillPayload
public byte[] getWillPayload()Returns the Will Message payload.The application message payload that the server MUST publish to the Will Topic when the will is triggered. In MQTT 3.1.1 this is limited to 65,535 bytes.
- Returns:
- the Will Message payload, or null if no will is set
- See Also:
-
setWillPayload
public void setWillPayload(byte[] willPayload) Sets the Will Message payload.- Parameters:
willPayload- the payload for the Will Message- See Also:
-
getWillProperties
Returns the Will Properties (MQTT 5.0 only).Will Properties appear in the CONNECT payload before the Will Topic when the Will Flag is set. They may include Will Delay Interval, Payload Format Indicator, Message Expiry Interval, Content Type, Response Topic, and Correlation Data.
- Returns:
- the Will Properties, or null if not set
- See Also:
-
setWillProperties
Sets the Will Properties (MQTT 5.0 only).- Parameters:
willProperties- the Will Properties- See Also:
-
getUsername
Returns the User Name.Present in the payload if the User Name Flag (bit 7 of the Connect Flags) is set. Used for authentication; the server MAY use this for authorization as well.
- Returns:
- the user name, or null if not provided
- See Also:
-
setUsername
Sets the User Name.- Parameters:
username- the user name- See Also:
-
getPassword
public byte[] getPassword()Returns the Password.Present in the payload if the Password Flag (bit 6 of the Connect Flags) is set. In MQTT 3.1.1, the User Name Flag MUST be set if the Password Flag is set. In MQTT 5.0, a password may be sent without a user name.
- Returns:
- the password as a byte array, or null if not provided
- See Also:
-
setPassword
public void setPassword(byte[] password) Sets the Password.- Parameters:
password- the password as a byte array- See Also:
-
toString
-