messagev5

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

Package message is an encoder/decoder library for MQTT 3.1 and 3.1.1 messages. You can find the MQTT specs at the following locations:

3.1.1 - http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/ 3.1 - http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html

From the spec:

MQTT is a Client Server publish/subscribe messaging transport protocol. It is light weight, open, simple, and designed so as to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.

The MQTT protocol works by exchanging a series of MQTT messages in a defined way. The protocol runs over TCP/IP, or over other network protocols that provide ordered, lossless, bi-directional connections.

There are two main items to take note in this package. The first is

	type MessageType byte

MessageType is the type representing the MQTT packet types. In the MQTT spec, MQTT control packet type is represented as a 4-bit unsigned value. MessageType receives several methods that returns string representations of the names and descriptions.

Also, one of the methods is New(). It returns a new Message object based on the mtype parameter. For example:

	m, err := CONNECT.New()
	msg := m.(*ConnectMessage)

This would return a PublishMessage struct, but mapped to the Message interface. You can then type assert it back to a *PublishMessage. Another way to create a new PublishMessage is to call

	msg := NewConnectMessage()

Every message type has a New function that returns a new message. The list of available message types are defined as constants below.

As you may have noticed, the second important item is the Message interface. It defines several methods that are common to all messages, including Name(), Desc(), and Type(). Most importantly, it also defines the Encode() and Decode() methods.

	Encode() (io.Reader, int, error)
	Decode(io.Reader) (int, error)

Encode returns an io.Reader in which the encoded bytes can be read. The second return value is the number of bytes encoded, so the caller knows how many bytes there will be. If Encode returns an error, then the first two return values should be considered invalid. Any changes to the message after Encode() is called will invalidate the io.Reader.

Decode reads from the io.Reader parameter until a full message is decoded, or when io.Reader returns EOF or error. The first return value is the number of bytes read from io.Reader. The second is error if Decode encounters any problems.

With these in mind, we can now do:

	// Create a new CONNECT message
	msg := NewConnectMessage()

	// Set the appropriate parameters
	msg.SetWillQos(1)
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte("surgemq"))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte("surgemq"))
	msg.SetPassword([]byte("verysecret"))

	// Encode the message and get the io.Reader
	r, n, err := msg.Encode()
	if err == nil {
		return err
	}

	// Write n bytes into the connection
	m, err := io.CopyN(conn, r, int64(n))
	if err != nil {
		return err
	}

	fmt.Printf("Sent %d bytes of %s message", m, msg.Name())

To receive a CONNECT message from a connection, we can do:

	// Create a new CONNECT message
	msg := NewConnectMessage()

	// Decode the message by reading from conn
	n, err := msg.Decode(conn)

If you don't know what type of message is coming down the pipe, you can do something like this:

	// Create a buffered IO reader for the connection
	br := bufio.NewReader(conn)

	// Peek at the first byte, which contains the message type
	b, err := br.Peek(1)
	if err != nil {
		return err
	}

	// Extract the type from the first byte
	t := MessageType(b[0] >> 4)

	// Create a new message
	msg, err := t.New()
	if err != nil {
		return err
	}

	// Decode it from the bufio.Reader
	n, err := msg.Decode(br)
	if err != nil {
		return err
	}

Documentation

Overview

Package message is an encoder/decoder library for MQTT 3.1 and 3.1.1 messages. You can find the MQTT specs at the following locations:

3.1.1 - http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/
3.1 - http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html

From the spec:

MQTT is a Client Server publish/subscribe messaging transport protocol. It is
light weight, open, simple, and designed so as to be easy to implement. These
characteristics make it ideal for use in many situations, including constrained
environments such as for communication in Machine to Machine (M2M) and Internet
of Things (IoT) contexts where a small code footprint is required and/or network
bandwidth is at a premium.

The MQTT protocol works by exchanging a series of MQTT messages in a defined way.
The protocol runs over TCP/IP, or over other network protocols that provide
ordered, lossless, bi-directional connections.

There are two main items to take note in this package. The first is

type MessageType byte

MessageType is the type representing the MQTT packet types. In the MQTT spec, MQTT control packet type is represented as a 4-bit unsigned value. MessageType receives several methods that returns string representations of the names and descriptions.

Also, one of the methods is New(). It returns a new Message object based on the mtype parameter. For example:

m, err := CONNECT.New()
msg := m.(*ConnectMessage)

This would return a PublishMessage struct, but mapped to the Message interface. You can then type assert it back to a *PublishMessage. Another way to create a new PublishMessage is to call

msg := NewConnectMessage()

Every message type has a New function that returns a new message. The list of available message types are defined as constants below.

As you may have noticed, the second important item is the Message interface. It defines several methods that are common to all messages, including Name(), Desc(), and Type(). Most importantly, it also defines the Encode() and Decode() methods.

Encode() (io.Reader, int, error)
Decode(io.Reader) (int, error)

Encode returns an io.Reader in which the encoded bytes can be read. The second return value is the number of bytes encoded, so the caller knows how many bytes there will be. If Encode returns an error, then the first two return values should be considered invalid. Any changes to the message after Encode() is called will invalidate the io.Reader.

Decode reads from the io.Reader parameter until a full message is decoded, or when io.Reader returns EOF or error. The first return value is the number of bytes read from io.Reader. The second is error if Decode encounters any problems.

With these in mind, we can now do:

// Create a new CONNECT message
msg := NewConnectMessage()

// Set the appropriate parameters
msg.SetWillQos(1)
msg.SetVersion(4)
msg.SetCleanSession(true)
msg.SetClientId([]byte("surgemq"))
msg.SetKeepAlive(10)
msg.SetWillTopic([]byte("will"))
msg.SetWillMessage([]byte("send me home"))
msg.SetUsername([]byte("surgemq"))
msg.SetPassword([]byte("verysecret"))

// Encode the message and get the io.Reader
r, n, err := msg.Encode()
if err == nil {
	return err
}

// Write n bytes into the connection
m, err := io.CopyN(conn, r, int64(n))
if err != nil {
	return err
}

fmt.Printf("Sent %d bytes of %s message", m, msg.Name())

To receive a CONNECT message from a connection, we can do:

// Create a new CONNECT message
msg := NewConnectMessage()

// Decode the message by reading from conn
n, err := msg.Decode(conn)

If you don't know what type of message is coming down the pipe, you can do something like this:

// Create a buffered IO reader for the connection
br := bufio.NewReader(conn)

// Peek at the first byte, which contains the message type
b, err := br.Peek(1)
if err != nil {
	return err
}

// Extract the type from the first byte
t := MessageType(b[0] >> 4)

// Create a new message
msg, err := t.New()
if err != nil {
	return err
}

// Decode it from the bufio.Reader
n, err := msg.Decode(br)
if err != nil {
	return err
}

Index

Constants

View Source
const (
	// QoS 0: At most once delivery
	// The message is delivered according to the capabilities of the underlying network.
	// No response is sent by the receiver and no retry is performed by the sender. The
	// message arrives at the receiver either once or not at all.
	QosAtMostOnce byte = iota

	// QoS 1: At least once delivery
	// This quality of service ensures that the message arrives at the receiver at least once.
	// A QoS 1 PUBLISH Packet has a Packet Identifier in its variable header and is acknowledged
	// by a PUBACK Packet. Section 2.3.1 provides more information about Packet Identifiers.
	QosAtLeastOnce

	// QoS 2: Exactly once delivery
	// This is the highest quality of service, for use when neither loss nor duplication of
	// messages are acceptable. There is an increased overhead associated with this quality of
	// service.
	QosExactlyOnce

	// QosFailure is a return value for a subscription if there's a problem while subscribing
	// to a specific topic.
	QosFailure = 0x80
)

Variables

View Source
var SupportedVersions map[byte]string = map[byte]string{
	0x3: "MQIsdp",
	0x4: "MQTT",
	0x5: "MQTT",
}

SupportedVersions is a map of the version number (0x3 or 0x4) to the version string, "MQIsdp" for 0x3, and "MQTT" for 0x4.

Functions

func ValidAuthReasonCode

func ValidAuthReasonCode(code ReasonCode) bool

ValidAuthReasonCode 验证auth原因码

func ValidConnAckReasonCode

func ValidConnAckReasonCode(code ReasonCode) bool

ValidConnAckReasonCode 验证ConnAck原因码

func ValidDisconnectReasonCode

func ValidDisconnectReasonCode(code ReasonCode) bool

ValidDisconnectReasonCode 验证disconnect原因码

func ValidPubAckReasonCode

func ValidPubAckReasonCode(code ReasonCode) bool

ValidPubAckReasonCode 验证PubAck原因码

func ValidPubCompReasonCode

func ValidPubCompReasonCode(code ReasonCode) bool

ValidPubCompReasonCode 验证PubComp原因码

func ValidPubRecReasonCode

func ValidPubRecReasonCode(code ReasonCode) bool

ValidPubRecReasonCode 验证PubRec原因码

func ValidPubRelReasonCode

func ValidPubRelReasonCode(code ReasonCode) bool

ValidPubRelReasonCode 验证PubRel原因码

func ValidQos

func ValidQos(qos byte) bool

ValidQos checks the QoS value to see if it's valid. Valid QoS are QosAtMostOnce, QosAtLeastonce, and QosExactlyOnce.

func ValidSubAckReasonCode

func ValidSubAckReasonCode(code ReasonCode) bool

ValidSubAckReasonCode 验证SubAck原因码

func ValidTopic

func ValidTopic(topic []byte) bool

ValidTopic checks the topic, which is a slice of bytes, to see if it's valid. Topic is considered valid if it's longer than 0 bytes, and doesn't contain any wildcard characters such as + and #. ValidTopic检查主题,这是一个字节片,看它是否有效。主题是 如果大于0字节且不包含任何通配符,则认为是有效的 例如+和#。

func ValidUnSubAckReasonCode

func ValidUnSubAckReasonCode(code ReasonCode) bool

ValidUnSubAckReasonCode 验证UnSubAck原因码

func ValidVersion

func ValidVersion(v byte) bool

ValidVersion checks to see if the version is valid. Current supported versions include 0x3 and 0x4.

Types

type AuthMessage

type AuthMessage struct {
	// contains filtered or unexported fields
}

AuthMessage v5版本新增

func NewAuthMessage

func NewAuthMessage() *AuthMessage

NewAuthMessage creates a new AUTH message.

func (*AuthMessage) AddUserProperty

func (this *AuthMessage) AddUserProperty(userProperty []byte)

func (*AuthMessage) AddUserPropertys

func (this *AuthMessage) AddUserPropertys(userProperty [][]byte)

func (*AuthMessage) AuthData

func (this *AuthMessage) AuthData() []byte

func (*AuthMessage) AuthMethod

func (this *AuthMessage) AuthMethod() []byte

func (*AuthMessage) Decode

func (this *AuthMessage) Decode(src []byte) (int, error)

func (*AuthMessage) Desc

func (this *AuthMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*AuthMessage) Encode

func (this *AuthMessage) Encode(dst []byte) (int, error)

func (*AuthMessage) Flags

func (this *AuthMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*AuthMessage) Len

func (this *AuthMessage) Len() int

func (*AuthMessage) MtypeFlags

func (this *AuthMessage) MtypeFlags() byte

func (*AuthMessage) Name

func (this *AuthMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*AuthMessage) PacketId

func (this *AuthMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*AuthMessage) PropertiesLen

func (this *AuthMessage) PropertiesLen() uint32

func (*AuthMessage) ReasonCode

func (this *AuthMessage) ReasonCode() ReasonCode

func (*AuthMessage) ReasonStr

func (this *AuthMessage) ReasonStr() []byte

func (*AuthMessage) RemainingLength

func (this *AuthMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*AuthMessage) SetAuthData

func (this *AuthMessage) SetAuthData(authData []byte)

func (*AuthMessage) SetAuthMethod

func (this *AuthMessage) SetAuthMethod(authMethod []byte)

func (*AuthMessage) SetMtypeFlags

func (this *AuthMessage) SetMtypeFlags(mtypeflags byte)

func (*AuthMessage) SetPacketId

func (this *AuthMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*AuthMessage) SetPropertiesLen

func (this *AuthMessage) SetPropertiesLen(propertiesLen uint32)

func (*AuthMessage) SetReasonCode

func (this *AuthMessage) SetReasonCode(reasonCode ReasonCode)

func (*AuthMessage) SetReasonStr

func (this *AuthMessage) SetReasonStr(reasonStr []byte)

func (*AuthMessage) SetRemainingLength

func (this *AuthMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*AuthMessage) SetType

func (this *AuthMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (AuthMessage) String

func (this AuthMessage) String() string

func (*AuthMessage) Type

func (this *AuthMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*AuthMessage) UserProperty

func (this *AuthMessage) UserProperty() [][]byte

type ConnackMessage

type ConnackMessage struct {
	// contains filtered or unexported fields
}

ConnackMessage The CONNACK Packet is the packet sent by the Server in response to a CONNECT Packet received from a Client. The first packet sent from the Server to the Client MUST be a CONNACK Packet [MQTT-3.2.0-1].

If the Client does not receive a CONNACK Packet from the Server within a reasonable amount of time, the Client SHOULD close the Network Connection. A "reasonable" amount of time depends on the type of application and the communications infrastructure.

func NewConnackMessage

func NewConnackMessage() *ConnackMessage

NewConnackMessage creates a new CONNACK message

func (*ConnackMessage) AddUserProperty

func (this *ConnackMessage) AddUserProperty(userProperty []byte)

func (*ConnackMessage) AddUserPropertys

func (this *ConnackMessage) AddUserPropertys(userProperty [][]byte)

func (*ConnackMessage) AssignedIdentifier

func (this *ConnackMessage) AssignedIdentifier() []byte

func (*ConnackMessage) AuthData

func (this *ConnackMessage) AuthData() []byte

func (*ConnackMessage) AuthMethod

func (this *ConnackMessage) AuthMethod() []byte

func (*ConnackMessage) Decode

func (this *ConnackMessage) Decode(src []byte) (int, error)

func (*ConnackMessage) Desc

func (this *ConnackMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*ConnackMessage) Encode

func (this *ConnackMessage) Encode(dst []byte) (int, error)

func (*ConnackMessage) Flags

func (this *ConnackMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*ConnackMessage) Len

func (this *ConnackMessage) Len() int

func (*ConnackMessage) MaxPacketSize

func (this *ConnackMessage) MaxPacketSize() uint32

func (*ConnackMessage) MaxQos

func (this *ConnackMessage) MaxQos() byte

func (*ConnackMessage) MtypeFlags

func (this *ConnackMessage) MtypeFlags() byte

func (*ConnackMessage) Name

func (this *ConnackMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*ConnackMessage) PacketId

func (this *ConnackMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*ConnackMessage) PropertiesLen

func (this *ConnackMessage) PropertiesLen() uint32

func (*ConnackMessage) ReasonCode

func (this *ConnackMessage) ReasonCode() ReasonCode

ReturnCode returns the return code received for the CONNECT message. The return type is an error

func (*ConnackMessage) ReasonStr

func (this *ConnackMessage) ReasonStr() []byte

func (*ConnackMessage) ReceiveMaximum

func (this *ConnackMessage) ReceiveMaximum() uint16

func (*ConnackMessage) RemainingLength

func (this *ConnackMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*ConnackMessage) ResponseInformation

func (this *ConnackMessage) ResponseInformation() []byte

func (*ConnackMessage) RetainAvailable

func (this *ConnackMessage) RetainAvailable() byte

func (*ConnackMessage) ServerKeepAlive

func (this *ConnackMessage) ServerKeepAlive() uint16

func (*ConnackMessage) ServerReference

func (this *ConnackMessage) ServerReference() []byte

func (*ConnackMessage) SessionExpiryInterval

func (this *ConnackMessage) SessionExpiryInterval() uint32

func (*ConnackMessage) SessionPresent

func (this *ConnackMessage) SessionPresent() bool

SessionPresent returns the session present flag value

func (*ConnackMessage) SetAssignedIdentifier

func (this *ConnackMessage) SetAssignedIdentifier(assignedIdentifier []byte)

func (*ConnackMessage) SetAuthData

func (this *ConnackMessage) SetAuthData(authData []byte)

func (*ConnackMessage) SetAuthMethod

func (this *ConnackMessage) SetAuthMethod(authMethod []byte)

func (*ConnackMessage) SetMaxPacketSize

func (this *ConnackMessage) SetMaxPacketSize(maxPacketSize uint32)

func (*ConnackMessage) SetMaxQos

func (this *ConnackMessage) SetMaxQos(maxQos byte)

func (*ConnackMessage) SetMtypeFlags

func (this *ConnackMessage) SetMtypeFlags(mtypeflags byte)

func (*ConnackMessage) SetPacketId

func (this *ConnackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*ConnackMessage) SetPropertiesLen

func (this *ConnackMessage) SetPropertiesLen(propertiesLen uint32)

func (*ConnackMessage) SetReasonCode

func (this *ConnackMessage) SetReasonCode(ret ReasonCode)

func (*ConnackMessage) SetReasonStr

func (this *ConnackMessage) SetReasonStr(reasonStr []byte)

func (*ConnackMessage) SetReceiveMaximum

func (this *ConnackMessage) SetReceiveMaximum(receiveMaximum uint16)

func (*ConnackMessage) SetRemainingLength

func (this *ConnackMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*ConnackMessage) SetResponseInformation

func (this *ConnackMessage) SetResponseInformation(responseInformation []byte)

func (*ConnackMessage) SetRetainAvailable

func (this *ConnackMessage) SetRetainAvailable(retainAvailable byte)

func (*ConnackMessage) SetServerKeepAlive

func (this *ConnackMessage) SetServerKeepAlive(serverKeepAlive uint16)

func (*ConnackMessage) SetServerReference

func (this *ConnackMessage) SetServerReference(serverReference []byte)

func (*ConnackMessage) SetSessionExpiryInterval

func (this *ConnackMessage) SetSessionExpiryInterval(sessionExpiryInterval uint32)

func (*ConnackMessage) SetSessionPresent

func (this *ConnackMessage) SetSessionPresent(v bool)

SetSessionPresent sets the value of the session present flag SetSessionPresent设置会话present标志的值

func (*ConnackMessage) SetSharedSubscriptionAvailable

func (this *ConnackMessage) SetSharedSubscriptionAvailable(sharedSubscriptionAvailable byte)

func (*ConnackMessage) SetSubscriptionIdentifierAvailable

func (this *ConnackMessage) SetSubscriptionIdentifierAvailable(subscriptionIdentifierAvailable byte)

func (*ConnackMessage) SetTopicAliasMax

func (this *ConnackMessage) SetTopicAliasMax(topicAliasMax uint16)

func (*ConnackMessage) SetType

func (this *ConnackMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (*ConnackMessage) SetWildcardSubscriptionAvailable

func (this *ConnackMessage) SetWildcardSubscriptionAvailable(wildcardSubscriptionAvailable byte)

func (*ConnackMessage) SharedSubscriptionAvailable

func (this *ConnackMessage) SharedSubscriptionAvailable() byte

func (ConnackMessage) String

func (this ConnackMessage) String() string

String returns a string representation of the CONNACK message

func (*ConnackMessage) SubscriptionIdentifierAvailable

func (this *ConnackMessage) SubscriptionIdentifierAvailable() byte

func (*ConnackMessage) TopicAliasMax

func (this *ConnackMessage) TopicAliasMax() uint16

func (*ConnackMessage) Type

func (this *ConnackMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*ConnackMessage) UserProperties

func (this *ConnackMessage) UserProperties() [][]byte

func (*ConnackMessage) WildcardSubscriptionAvailable

func (this *ConnackMessage) WildcardSubscriptionAvailable() byte

type ConnectMessage

type ConnectMessage struct {
	// contains filtered or unexported fields
}

ConnectMessage After a Network Connection is established by a Client to a Server, the first Packet sent from the Client to the Server MUST be a CONNECT Packet [MQTT-3.1.0-1].

A Client can only send the CONNECT Packet once over a Network Connection. The Server MUST process a second CONNECT Packet sent from a Client as a protocol violation and disconnect the Client [MQTT-3.1.0-2]. See section 4.8 for information about handling errors.

func NewConnectMessage

func NewConnectMessage() *ConnectMessage

NewConnectMessage creates a new CONNECT message.

func (*ConnectMessage) AddUserProperty

func (this *ConnectMessage) AddUserProperty(userProperty []byte)

func (*ConnectMessage) AddUserPropertys

func (this *ConnectMessage) AddUserPropertys(userProperty [][]byte)

func (*ConnectMessage) AddWillUserProperty

func (this *ConnectMessage) AddWillUserProperty(willUserProperty []byte)

func (*ConnectMessage) AddWillUserPropertys

func (this *ConnectMessage) AddWillUserPropertys(willUserProperty [][]byte)

func (*ConnectMessage) AuthData

func (this *ConnectMessage) AuthData() []byte

func (*ConnectMessage) AuthMethod

func (this *ConnectMessage) AuthMethod() []byte

func (*ConnectMessage) CleanSession

func (this *ConnectMessage) CleanSession() bool

CleanSession returns the bit that specifies the handling of the Session state. The Client and Server can store Session state to enable reliable messaging to continue across a sequence of Network Connections. This bit is used to control the lifetime of the Session state. CleanSession返回指定会话状态处理的位。 客户端和服务器可以存储会话状态,以实现可靠的消息传递 继续通过网络连接序列。这个位用来控制 会话状态的生存期。

func (*ConnectMessage) CleanStart

func (this *ConnectMessage) CleanStart() bool

func (*ConnectMessage) ClientId

func (this *ConnectMessage) ClientId() []byte

ClientId returns an ID that identifies the Client to the Server. Each Client connecting to the Server has a unique ClientId. The ClientId MUST be used by Clients and by Servers to identify state that they hold relating to this MQTT Session between the Client and the Server

func (*ConnectMessage) ContentType

func (this *ConnectMessage) ContentType() []byte

func (*ConnectMessage) CorrelationData

func (this *ConnectMessage) CorrelationData() []byte

func (*ConnectMessage) Decode

func (this *ConnectMessage) Decode(src []byte) (int, error)

Decode For the CONNECT message, the error returned could be a ConnackReturnCode, so be sure to check that. Otherwise it's a generic error. If a generic error is returned, this Message should be considered invalid.

Caller should call ValidConnackError(err) to see if the returned error is a Connack error. If so, caller should send the Client back the corresponding CONNACK message.

func (*ConnectMessage) Desc

func (this *ConnectMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*ConnectMessage) Encode

func (this *ConnectMessage) Encode(dst []byte) (int, error)

func (*ConnectMessage) Flags

func (this *ConnectMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*ConnectMessage) KeepAlive

func (this *ConnectMessage) KeepAlive() uint16

KeepAlive returns a time interval measured in seconds. Expressed as a 16-bit word, it is the maximum time interval that is permitted to elapse between the point at which the Client finishes transmitting one Control Packet and the point it starts sending the next.

func (*ConnectMessage) Len

func (this *ConnectMessage) Len() int

func (*ConnectMessage) MaxPacketSize

func (this *ConnectMessage) MaxPacketSize() uint32

func (*ConnectMessage) MtypeFlags

func (this *ConnectMessage) MtypeFlags() byte

func (*ConnectMessage) Name

func (this *ConnectMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*ConnectMessage) PacketId

func (this *ConnectMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*ConnectMessage) Password

func (this *ConnectMessage) Password() []byte

Password returns the password from the payload. If the Password Flag is set to 1, this must be in the payload. It can be used by the Server for authentication and authorization.

func (*ConnectMessage) PasswordFlag

func (this *ConnectMessage) PasswordFlag() bool

PasswordFlag returns the bit that specifies whether a password is present in the payload.

func (*ConnectMessage) PayloadFormatIndicator

func (this *ConnectMessage) PayloadFormatIndicator() byte

func (*ConnectMessage) PropertiesLen

func (this *ConnectMessage) PropertiesLen() uint32

func (*ConnectMessage) ReceiveMaximum

func (this *ConnectMessage) ReceiveMaximum() uint16

func (*ConnectMessage) RemainingLength

func (this *ConnectMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*ConnectMessage) RequestProblemInfo

func (this *ConnectMessage) RequestProblemInfo() byte

func (*ConnectMessage) RequestRespInfo

func (this *ConnectMessage) RequestRespInfo() byte

func (*ConnectMessage) ResponseTopic

func (this *ConnectMessage) ResponseTopic() []byte

func (*ConnectMessage) SessionExpiryInterval

func (this *ConnectMessage) SessionExpiryInterval() uint32

func (*ConnectMessage) SetAuthData

func (this *ConnectMessage) SetAuthData(authData []byte)

func (*ConnectMessage) SetAuthMethod

func (this *ConnectMessage) SetAuthMethod(authMethod []byte)

func (*ConnectMessage) SetCleanSession

func (this *ConnectMessage) SetCleanSession(v bool)

SetCleanSession sets the bit that specifies the handling of the Session state.

func (*ConnectMessage) SetClientId

func (this *ConnectMessage) SetClientId(v []byte) error

SetClientId sets an ID that identifies the Client to the Server.

func (*ConnectMessage) SetContentType

func (this *ConnectMessage) SetContentType(contentType []byte)

func (*ConnectMessage) SetCorrelationData

func (this *ConnectMessage) SetCorrelationData(correlationData []byte)

func (*ConnectMessage) SetKeepAlive

func (this *ConnectMessage) SetKeepAlive(v uint16)

SetKeepAlive sets the time interval in which the server should keep the connection alive.

func (*ConnectMessage) SetMaxPacketSize

func (this *ConnectMessage) SetMaxPacketSize(maxPacketSize uint32)

func (*ConnectMessage) SetMtypeFlags

func (this *ConnectMessage) SetMtypeFlags(mtypeflags byte)

func (*ConnectMessage) SetPacketId

func (this *ConnectMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*ConnectMessage) SetPassword

func (this *ConnectMessage) SetPassword(v []byte)

SetPassword sets the username for authentication.

func (*ConnectMessage) SetPasswordFlag

func (this *ConnectMessage) SetPasswordFlag(v bool)

SetPasswordFlag sets the bit that specifies whether a password is present in the payload.

func (*ConnectMessage) SetPayloadFormatIndicator

func (this *ConnectMessage) SetPayloadFormatIndicator(payloadFormatIndicator byte)

func (*ConnectMessage) SetPropertiesLen

func (this *ConnectMessage) SetPropertiesLen(propertiesLen uint32)

func (*ConnectMessage) SetReceiveMaximum

func (this *ConnectMessage) SetReceiveMaximum(receiveMaximum uint16)

func (*ConnectMessage) SetRemainingLength

func (this *ConnectMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*ConnectMessage) SetRequestProblemInfo

func (this *ConnectMessage) SetRequestProblemInfo(requestProblemInfo byte)

func (*ConnectMessage) SetRequestRespInfo

func (this *ConnectMessage) SetRequestRespInfo(requestRespInfo byte)

func (*ConnectMessage) SetResponseTopic

func (this *ConnectMessage) SetResponseTopic(responseTopic []byte)

func (*ConnectMessage) SetSessionExpiryInterval

func (this *ConnectMessage) SetSessionExpiryInterval(sessionExpiryInterval uint32)

func (*ConnectMessage) SetTopicAliasMax

func (this *ConnectMessage) SetTopicAliasMax(topicAliasMax uint16)

func (*ConnectMessage) SetType

func (this *ConnectMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (*ConnectMessage) SetUsername

func (this *ConnectMessage) SetUsername(v []byte)

SetUsername sets the username for authentication.

func (*ConnectMessage) SetUsernameFlag

func (this *ConnectMessage) SetUsernameFlag(v bool)

SetUsernameFlag sets the bit that specifies whether a user name is present in the payload.

func (*ConnectMessage) SetVersion

func (this *ConnectMessage) SetVersion(v byte) error

SetVersion sets the version value of the CONNECT message

func (*ConnectMessage) SetWillDelayInterval

func (this *ConnectMessage) SetWillDelayInterval(willDelayInterval uint32)

func (*ConnectMessage) SetWillFlag

func (this *ConnectMessage) SetWillFlag(v bool)

SetWillFlag sets the bit that specifies whether a Will Message should be stored on the server.

func (*ConnectMessage) SetWillMessage

func (this *ConnectMessage) SetWillMessage(v []byte)

SetWillMessage sets the Will Message that is to be published to the Will Topic.

func (*ConnectMessage) SetWillMsgExpiryInterval

func (this *ConnectMessage) SetWillMsgExpiryInterval(willMsgExpiryInterval uint32)

func (*ConnectMessage) SetWillPropertiesLen

func (this *ConnectMessage) SetWillPropertiesLen(willPropertiesLen uint32)

func (*ConnectMessage) SetWillQos

func (this *ConnectMessage) SetWillQos(qos byte) error

SetWillQos sets the two bits that specify the QoS level to be used when publishing the Will Message.

func (*ConnectMessage) SetWillRetain

func (this *ConnectMessage) SetWillRetain(v bool)

SetWillRetain sets the bit specifies if the Will Message is to be Retained when it is published.

func (*ConnectMessage) SetWillTopic

func (this *ConnectMessage) SetWillTopic(v []byte)

SetWillTopic sets the topic in which the Will Message should be published to.

func (ConnectMessage) String

func (this ConnectMessage) String() string

String returns a string representation of the CONNECT message

func (*ConnectMessage) TopicAliasMax

func (this *ConnectMessage) TopicAliasMax() uint16

func (*ConnectMessage) Type

func (this *ConnectMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*ConnectMessage) UserProperty

func (this *ConnectMessage) UserProperty() [][]byte

func (*ConnectMessage) Username

func (this *ConnectMessage) Username() []byte

Username returns the username from the payload. If the User Name Flag is set to 1, this must be in the payload. It can be used by the Server for authentication and authorization.

func (*ConnectMessage) UsernameFlag

func (this *ConnectMessage) UsernameFlag() bool

UsernameFlag returns the bit that specifies whether a user name is present in the payload.

func (*ConnectMessage) Version

func (this *ConnectMessage) Version() byte

Version returns the the 8 bit unsigned value that represents the revision level of the protocol used by the Client. The value of the Protocol Level field for the version 3.1.1 of the protocol is 4 (0x04).

func (*ConnectMessage) WillDelayInterval

func (this *ConnectMessage) WillDelayInterval() uint32

func (*ConnectMessage) WillFlag

func (this *ConnectMessage) WillFlag() bool

WillFlag returns the bit that specifies whether a Will Message should be stored on the server. If the Will Flag is set to 1 this indicates that, if the Connect request is accepted, a Will Message MUST be stored on the Server and associated with the Network Connection. WillFlag返回指定是否存储Will消息的位 在服务器上。如果Will标志设置为1,这表示如果连接 请求被接受,一个Will消息必须存储在服务器上并关联 与网络连接。

func (*ConnectMessage) WillMessage

func (this *ConnectMessage) WillMessage() []byte

WillMessage returns the Will Message that is to be published to the Will Topic.

func (*ConnectMessage) WillMsgExpiryInterval

func (this *ConnectMessage) WillMsgExpiryInterval() uint32

func (*ConnectMessage) WillPropertiesLen

func (this *ConnectMessage) WillPropertiesLen() uint32

func (*ConnectMessage) WillQos

func (this *ConnectMessage) WillQos() byte

WillQos returns the two bits that specify the QoS level to be used when publishing the Will Message.

func (*ConnectMessage) WillRetain

func (this *ConnectMessage) WillRetain() bool

WillRetain returns the bit specifies if the Will Message is to be Retained when it is published. Will retain返回指定Will消息是否被保留的位 出版。

func (*ConnectMessage) WillTopic

func (this *ConnectMessage) WillTopic() []byte

WillTopic returns the topic in which the Will Message should be published to. If the Will Flag is set to 1, the Will Topic must be in the payload.

func (*ConnectMessage) WillUserProperty

func (this *ConnectMessage) WillUserProperty() [][]byte

type DisconnectMessage

type DisconnectMessage struct {
	// contains filtered or unexported fields
}

The DISCONNECT Packet is the final Control Packet sent from the Client to the Server. It indicates that the Client is disconnecting cleanly.

func NewDisconnectMessage

func NewDisconnectMessage() *DisconnectMessage

NewDisconnectMessage creates a new DISCONNECT message.

func (*DisconnectMessage) AddUserProperty

func (this *DisconnectMessage) AddUserProperty(userProperty []byte)

func (*DisconnectMessage) AddUserPropertys

func (this *DisconnectMessage) AddUserPropertys(userProperty [][]byte)

func (*DisconnectMessage) Decode

func (this *DisconnectMessage) Decode(src []byte) (int, error)

func (*DisconnectMessage) Desc

func (this *DisconnectMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*DisconnectMessage) Encode

func (this *DisconnectMessage) Encode(dst []byte) (int, error)

func (*DisconnectMessage) Flags

func (this *DisconnectMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*DisconnectMessage) Len

func (this *DisconnectMessage) Len() int

func (*DisconnectMessage) MtypeFlags

func (this *DisconnectMessage) MtypeFlags() byte

func (*DisconnectMessage) Name

func (this *DisconnectMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*DisconnectMessage) PacketId

func (this *DisconnectMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*DisconnectMessage) PropertyLen

func (this *DisconnectMessage) PropertyLen() uint32

func (*DisconnectMessage) ReasonCode

func (this *DisconnectMessage) ReasonCode() ReasonCode

func (*DisconnectMessage) ReasonStr

func (this *DisconnectMessage) ReasonStr() []byte

func (*DisconnectMessage) RemainingLength

func (this *DisconnectMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*DisconnectMessage) ServerReference

func (this *DisconnectMessage) ServerReference() []byte

func (*DisconnectMessage) SessionExpiryInterval

func (this *DisconnectMessage) SessionExpiryInterval() uint32

func (*DisconnectMessage) SetMtypeFlags

func (this *DisconnectMessage) SetMtypeFlags(mtypeflags byte)

func (*DisconnectMessage) SetPacketId

func (this *DisconnectMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*DisconnectMessage) SetPropertyLen

func (this *DisconnectMessage) SetPropertyLen(propertyLen uint32)

func (*DisconnectMessage) SetReasonCode

func (this *DisconnectMessage) SetReasonCode(reasonCode ReasonCode)

func (*DisconnectMessage) SetReasonStr

func (this *DisconnectMessage) SetReasonStr(reasonStr []byte)

func (*DisconnectMessage) SetRemainingLength

func (this *DisconnectMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*DisconnectMessage) SetServerReference

func (this *DisconnectMessage) SetServerReference(serverReference []byte)

func (*DisconnectMessage) SetSessionExpiryInterval

func (this *DisconnectMessage) SetSessionExpiryInterval(sessionExpiryInterval uint32)

func (*DisconnectMessage) SetType

func (this *DisconnectMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (*DisconnectMessage) String

func (d *DisconnectMessage) String() string

func (*DisconnectMessage) Type

func (this *DisconnectMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*DisconnectMessage) UserProperty

func (this *DisconnectMessage) UserProperty() [][]byte

type Message

type Message interface {
	// Name returns a string representation of the message type. Examples include
	// "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of
	// the message types and cannot be changed.
	Name() string

	// Desc returns a string description of the message type. For example, a
	// CONNECT message would return "Client request to connect to Server." These
	// descriptions are statically defined (copied from the MQTT spec) and cannot
	// be changed.
	Desc() string

	// Type returns the MessageType of the Message. The retured value should be one
	// of the constants defined for MessageType.
	Type() MessageType

	// PacketId returns the packet ID of the Message. The retured value is 0 if
	// there's no packet ID for this message type. Otherwise non-0.
	PacketId() uint16

	// Encode writes the message bytes into the byte array from the argument. It
	// returns the number of bytes encoded and whether there's any errors along
	// the way. If there's any errors, then the byte slice and count should be
	// considered invalid.
	Encode([]byte) (int, error)

	// Decode reads the bytes in the byte slice from the argument. It returns the
	// total number of bytes decoded, and whether there's any errors during the
	// process. The byte slice MUST NOT be modified during the duration of this
	// message being available since the byte slice is internally stored for
	// references.
	Decode([]byte) (int, error)

	Len() int
}

Message is an interface defined for all MQTT message types.

type MessageType

type MessageType byte

MessageType is the type representing the MQTT packet types. In the MQTT spec, MQTT control packet type is represented as a 4-bit unsigned value. MessageType是表示MQTT数据包类型的类型。在MQTT规范中, MQTT控制包类型表示为4位无符号值。

const (
	// RESERVED is a reserved value and should be considered an invalid message type
	// RESERVED是一个保留值,应该被认为是一个无效的消息类型
	RESERVED MessageType = iota

	// CONNECT: Client to Server. Client request to connect to Server.
	// 连接到服务器。客户机请求连接到服务器。
	CONNECT

	// CONNACK: Server to Client. Connect acknowledgement.
	// 服务器到客户端。连接确认。
	CONNACK

	// PUBLISH: Client to Server, or Server to Client. Publish message.
	// 发布:客户端到服务器,或服务器到客户端。发布消息。
	PUBLISH

	// PUBACK: Client to Server, or Server to Client. Publish acknowledgment for
	// QoS 1 messages.
	// 客户端到服务器,或者服务器到客户端公开承认的
	// QoS 1 消息。
	PUBACK

	// PUBACK: Client to Server, or Server to Client. Publish received for QoS 2 messages.
	// Assured delivery part 1.
	// 客户端到服务器,或者服务器到客户端发布为QoS 2消息接收的消息。
	// 保证交货第一部分。
	PUBREC

	// PUBREL: Client to Server, or Server to Client. Publish release for QoS 2 messages.
	// Assured delivery part 1.
	// PUBREL:客户端到服务器,或者服务器到客户端发布发布QoS 2消息。
	// 保证交货第一部分。
	PUBREL

	// PUBCOMP: Client to Server, or Server to Client. Publish complete for QoS 2 messages.
	// Assured delivery part 3.
	// PUBCOMP:客户端到服务器,或者服务器到客户端。QoS 2消息的发布完成。
	// 保证交货第3部分。
	PUBCOMP

	// SUBSCRIBE: Client to Server. Client subscribe request.
	SUBSCRIBE

	// SUBACK: Server to Client. Subscribe acknowledgement.
	SUBACK

	// UNSUBSCRIBE: Client to Server. Unsubscribe request.
	UNSUBSCRIBE

	// UNSUBACK: Server to Client. Unsubscribe acknowlegment.
	// UNSUBACK:服务器到客户端。acknowlegment退订。
	UNSUBACK

	// PINGREQ: Client to Server. PING request.
	PINGREQ

	// PINGRESP: Server to Client. PING response.
	PINGRESP

	// DISCONNECT: Client to Server. Client is disconnecting.
	DISCONNECT
	// AUTH: Client to Server, AUTH
	// 两个方向都允许	认证信息交换
	AUTH
	// RESERVED2 is a reserved value and should be considered an invalid message type.
	// RESERVED2是一个保留值,应该被认为是一个无效的消息类型。
	// 两个RESERVED是方便做校验,直接判断是否处在这两个中间即可判断合法性
	RESERVED2
)

func (MessageType) DefaultFlags

func (this MessageType) DefaultFlags() byte

DefaultFlags returns the default flag values for the message type, as defined by the MQTT spec.

func (MessageType) Desc

func (this MessageType) Desc() string

Desc returns the description of the message type. It is statically defined (copied from MQTT spec) and cannot be changed.

func (MessageType) Name

func (this MessageType) Name() string

Name returns the name of the message type. It should correspond to one of the constant values defined for MessageType. It is statically defined and cannot be changed.

func (MessageType) New

func (this MessageType) New() (Message, error)

New creates a new message based on the message type. It is a shortcut to call one of the New*Message functions. If an error is returned then the message type is invalid.

func (MessageType) String

func (this MessageType) String() string

func (MessageType) Valid

func (this MessageType) Valid() bool

Valid returns a boolean indicating whether the message type is valid or not. Valid返回一个布尔值,该值指示消息类型是否有效。

type PingreqMessage

type PingreqMessage struct {
	// contains filtered or unexported fields
}

The PINGREQ Packet is sent from a Client to the Server. It can be used to:

  1. Indicate to the Server that the Client is alive in the absence of any other Control Packets being sent from the Client to the Server.
  2. Request that the Server responds to confirm that it is alive.
  3. Exercise the network to indicate that the Network Connection is active.

func NewPingreqMessage

func NewPingreqMessage() *PingreqMessage

NewPingreqMessage creates a new PINGREQ message.

func (*PingreqMessage) Decode

func (this *PingreqMessage) Decode(src []byte) (int, error)

func (*PingreqMessage) Desc

func (this *PingreqMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PingreqMessage) Encode

func (this *PingreqMessage) Encode(dst []byte) (int, error)

func (*PingreqMessage) Flags

func (this *PingreqMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PingreqMessage) Len

func (this *PingreqMessage) Len() int

func (*PingreqMessage) MtypeFlags

func (this *PingreqMessage) MtypeFlags() byte

func (*PingreqMessage) Name

func (this *PingreqMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PingreqMessage) PacketId

func (this *PingreqMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PingreqMessage) RemainingLength

func (this *PingreqMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PingreqMessage) SetMtypeFlags

func (this *PingreqMessage) SetMtypeFlags(mtypeflags byte)

func (*PingreqMessage) SetPacketId

func (this *PingreqMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PingreqMessage) SetRemainingLength

func (this *PingreqMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PingreqMessage) SetType

func (this *PingreqMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (PingreqMessage) String

func (this PingreqMessage) String() string

String returns a string representation of the message.

func (*PingreqMessage) Type

func (this *PingreqMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

type PingrespMessage

type PingrespMessage struct {
	// contains filtered or unexported fields
}

A PINGRESP Packet is sent by the Server to the Client in response to a PINGREQ Packet. It indicates that the Server is alive.

func NewPingrespMessage

func NewPingrespMessage() *PingrespMessage

NewPingrespMessage creates a new PINGRESP message.

func (*PingrespMessage) Decode

func (this *PingrespMessage) Decode(src []byte) (int, error)

func (*PingrespMessage) Desc

func (this *PingrespMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PingrespMessage) Encode

func (this *PingrespMessage) Encode(dst []byte) (int, error)

func (*PingrespMessage) Flags

func (this *PingrespMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PingrespMessage) Len

func (this *PingrespMessage) Len() int

func (*PingrespMessage) MtypeFlags

func (this *PingrespMessage) MtypeFlags() byte

func (*PingrespMessage) Name

func (this *PingrespMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PingrespMessage) PacketId

func (this *PingrespMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PingrespMessage) RemainingLength

func (this *PingrespMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PingrespMessage) SetMtypeFlags

func (this *PingrespMessage) SetMtypeFlags(mtypeflags byte)

func (*PingrespMessage) SetPacketId

func (this *PingrespMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PingrespMessage) SetRemainingLength

func (this *PingrespMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PingrespMessage) SetType

func (this *PingrespMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (PingrespMessage) String

func (this PingrespMessage) String() string

String returns a string representation of the message.

func (*PingrespMessage) Type

func (this *PingrespMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

type PropertyCode

type PropertyCode = byte

原因码

const (
	LoadFormatDescription                 PropertyCode = 0x01 // 载荷格式说明 字节 PUBLISH, Will Properties
	MessageExpirationTime                 PropertyCode = 0x02 // 消息过期时间 四字节整数 PUBLISH, Will Properties
	ContentType                           PropertyCode = 0x03 // 内容类型 UTF-8 编码字符串 PUBLISH, Will Properties
	ResponseTopic                         PropertyCode = 0x08 // 响应主题 UTF-8 编码字符串 PUBLISH, Will Properties
	RelatedData                           PropertyCode = 0x09 // 相关数据,对比数据 二进制数据 PUBLISH, Will Properties
	DefiningIdentifiers                   PropertyCode = 0x0B // 定义标识符 变长字节整数 PUBLISH, SUBSCRIBE
	SessionExpirationInterval             PropertyCode = 0x11 // 会话过期间隔 四字节整数 CONNECT, CONNACK, DISCONNECT
	AssignCustomerIdentifiers             PropertyCode = 0x12 // 分配客户标识符 UTF-8 编码字符串 CONNACK
	ServerSurvivalTime                    PropertyCode = 0x13 // 服务端保活时间 双字节整数 CONNACK
	AuthenticationMethod                  PropertyCode = 0x15 // 认证方法 UTF-8 编码字符串 CONNECT, CONNACK, AUTH
	AuthenticationData                    PropertyCode = 0x16 // 认证数据 二进制数据 CONNECT, CONNACK, AUTH
	RequestProblemInformation             PropertyCode = 0x17 // 请求问题信息 字节 CONNECT
	DelayWills                            PropertyCode = 0x18 // 遗嘱延时间隔 四字节整数 Will Propertie
	RequestResponseInformation            PropertyCode = 0x19 // 请求响应信息 字节 CONNECT
	SolicitedMessage                      PropertyCode = 0x1A // 请求信息 UTF-8 编码字符串 CONNACK
	ServerReference                       PropertyCode = 0x1C // 服务端参考 UTF-8 编码字符串 CONNACK, DISCONNECT
	ReasonString                          PropertyCode = 0x1F // 原因字符串 UTF-8 编码字符串 CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, DISCONNECT, AUTH
	MaximumQuantityReceived               PropertyCode = 0x21 // 接收最大数量 双字节整数 CONNECT, CONNACK
	MaximumLengthOfTopicAlias             PropertyCode = 0x22 // 主题别名最大长度 双字节整数 CONNECT, CONNACK
	ThemeAlias                            PropertyCode = 0x23 // 主题别名 双字节整数 PUBLISH
	MaximumQoS                            PropertyCode = 0x24 // 最大 QoS 字节 CONNACK
	PreservePropertyAvailability          PropertyCode = 0x25 // 保留属性可用性 字节 CONNACK
	UserProperty                          PropertyCode = 0x26 // 用户属性 UTF-8 字符串对 CONNECT, CONNACK, PUBLISH, Will Properties, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, DISCONNECT, AUTH
	MaximumMessageLength                  PropertyCode = 0x27 // 最大报文长度 四字节整数 CONNECT, CONNACK
	WildcardSubscriptionAvailability      PropertyCode = 0x28 // 通配符订阅可用性 字节 CONNACK
	AvailabilityOfSubscriptionIdentifiers PropertyCode = 0x29 // 订阅标识符可用性 字节 CONNACK
	SharedSubscriptionAvailability        PropertyCode = 0x2A // 共享订阅可用性 字节 CONNACK
)

type PubackMessage

type PubackMessage struct {
	// contains filtered or unexported fields
}

PubackMessage A PUBACK Packet is the response to a PUBLISH Packet with QoS level 1.

func NewPubackMessage

func NewPubackMessage() *PubackMessage

NewPubackMessage creates a new PUBACK message.

func (*PubackMessage) AddUserProperty

func (this *PubackMessage) AddUserProperty(userProperty []byte)

func (*PubackMessage) AddUserPropertys

func (this *PubackMessage) AddUserPropertys(userProperty [][]byte)

func (*PubackMessage) Decode

func (this *PubackMessage) Decode(src []byte) (int, error)

func (*PubackMessage) Desc

func (this *PubackMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PubackMessage) Encode

func (this *PubackMessage) Encode(dst []byte) (int, error)

func (*PubackMessage) Flags

func (this *PubackMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PubackMessage) Len

func (this *PubackMessage) Len() int

func (*PubackMessage) MtypeFlags

func (this *PubackMessage) MtypeFlags() byte

func (*PubackMessage) Name

func (this *PubackMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PubackMessage) PacketId

func (this *PubackMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PubackMessage) PropertyLen

func (this *PubackMessage) PropertyLen() uint32

func (*PubackMessage) ReasonCode

func (this *PubackMessage) ReasonCode() ReasonCode

func (*PubackMessage) ReasonStr

func (this *PubackMessage) ReasonStr() []byte

func (*PubackMessage) RemainingLength

func (this *PubackMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PubackMessage) SetMtypeFlags

func (this *PubackMessage) SetMtypeFlags(mtypeflags byte)

func (*PubackMessage) SetPacketId

func (this *PubackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PubackMessage) SetPropertyLen

func (this *PubackMessage) SetPropertyLen(propertyLen uint32)

func (*PubackMessage) SetReasonCode

func (this *PubackMessage) SetReasonCode(reasonCode ReasonCode)

func (*PubackMessage) SetReasonStr

func (this *PubackMessage) SetReasonStr(reasonStr []byte)

func (*PubackMessage) SetRemainingLength

func (this *PubackMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PubackMessage) SetType

func (this *PubackMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (PubackMessage) String

func (this PubackMessage) String() string

func (*PubackMessage) Type

func (this *PubackMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*PubackMessage) UserProperty

func (this *PubackMessage) UserProperty() [][]byte

type PubcompMessage

type PubcompMessage struct {
	PubackMessage
}

PubcompMessage The PUBCOMP Packet is the response to a PUBREL Packet. It is the fourth and final packet of the QoS 2 protocol exchange.

func NewPubcompMessage

func NewPubcompMessage() *PubcompMessage

NewPubcompMessage creates a new PUBCOMP message.

func (*PubcompMessage) Decode

func (this *PubcompMessage) Decode(src []byte) (int, error)

func (*PubcompMessage) Desc

func (this *PubcompMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PubcompMessage) Flags

func (this *PubcompMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PubcompMessage) MtypeFlags

func (this *PubcompMessage) MtypeFlags() byte

func (*PubcompMessage) Name

func (this *PubcompMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PubcompMessage) PacketId

func (this *PubcompMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PubcompMessage) RemainingLength

func (this *PubcompMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PubcompMessage) SetMtypeFlags

func (this *PubcompMessage) SetMtypeFlags(mtypeflags byte)

func (*PubcompMessage) SetPacketId

func (this *PubcompMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PubcompMessage) SetRemainingLength

func (this *PubcompMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PubcompMessage) SetType

func (this *PubcompMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (*PubcompMessage) Type

func (this *PubcompMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

type PublishMessage

type PublishMessage struct {
	// contains filtered or unexported fields
}

PublishMessage A PUBLISH Control Packet is sent from a Client to a Server or from Server to a Client to transport an Application Message.

func NewPublishMessage

func NewPublishMessage() *PublishMessage

NewPublishMessage creates a new PUBLISH message.

func (*PublishMessage) AddUserProperty

func (this *PublishMessage) AddUserProperty(userProperty []byte)

func (*PublishMessage) AddUserPropertys

func (this *PublishMessage) AddUserPropertys(userProperty [][]byte)

func (*PublishMessage) ContentType

func (this *PublishMessage) ContentType() []byte

func (*PublishMessage) CorrelationData

func (this *PublishMessage) CorrelationData() []byte

func (*PublishMessage) Decode

func (this *PublishMessage) Decode(src []byte) (int, error)

func (*PublishMessage) Desc

func (this *PublishMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PublishMessage) Dup

func (this *PublishMessage) Dup() bool

Dup returns the value specifying the duplicate delivery of a PUBLISH Control Packet. If the DUP flag is set to 0, it indicates that this is the first occasion that the Client or Server has attempted to send this MQTT PUBLISH Packet. If the DUP flag is set to 1, it indicates that this might be re-delivery of an earlier attempt to send the Packet.

func (*PublishMessage) Encode

func (this *PublishMessage) Encode(dst []byte) (int, error)

func (*PublishMessage) Flags

func (this *PublishMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PublishMessage) Len

func (this *PublishMessage) Len() int

func (*PublishMessage) MessageExpiryInterval

func (this *PublishMessage) MessageExpiryInterval() uint32

func (*PublishMessage) MtypeFlags

func (this *PublishMessage) MtypeFlags() byte

func (*PublishMessage) Name

func (this *PublishMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PublishMessage) PacketId

func (this *PublishMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PublishMessage) Payload

func (this *PublishMessage) Payload() []byte

Payload returns the application message that's part of the PUBLISH message.

func (*PublishMessage) PayloadFormatIndicator

func (this *PublishMessage) PayloadFormatIndicator() byte

func (*PublishMessage) PropertiesLen

func (this *PublishMessage) PropertiesLen() uint32

func (*PublishMessage) QoS

func (this *PublishMessage) QoS() byte

QoS returns the field that indicates the level of assurance for delivery of an Application Message. The values are QosAtMostOnce, QosAtLeastOnce and QosExactlyOnce.

func (*PublishMessage) RemainingLength

func (this *PublishMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PublishMessage) ResponseTopic

func (this *PublishMessage) ResponseTopic() []byte

func (*PublishMessage) Retain

func (this *PublishMessage) Retain() bool

Retain returns the value of the RETAIN flag. This flag is only used on the PUBLISH Packet. If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its topic name.

func (*PublishMessage) SetContentType

func (this *PublishMessage) SetContentType(contentType []byte)

func (*PublishMessage) SetCorrelationData

func (this *PublishMessage) SetCorrelationData(correlationData []byte)

func (*PublishMessage) SetDup

func (this *PublishMessage) SetDup(v bool)

SetDup sets the value specifying the duplicate delivery of a PUBLISH Control Packet.

func (*PublishMessage) SetMessageExpiryInterval

func (this *PublishMessage) SetMessageExpiryInterval(messageExpiryInterval uint32)

func (*PublishMessage) SetMtypeFlags

func (this *PublishMessage) SetMtypeFlags(mtypeflags byte)

func (*PublishMessage) SetPacketId

func (this *PublishMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PublishMessage) SetPayload

func (this *PublishMessage) SetPayload(v []byte)

SetPayload sets the application message that's part of the PUBLISH message.

func (*PublishMessage) SetPayloadFormatIndicator

func (this *PublishMessage) SetPayloadFormatIndicator(payloadFormatIndicator byte)

func (*PublishMessage) SetPropertiesLen

func (this *PublishMessage) SetPropertiesLen(propertiesLen uint32)

func (*PublishMessage) SetQoS

func (this *PublishMessage) SetQoS(v byte) error

SetQoS sets the field that indicates the level of assurance for delivery of an Application Message. The values are QosAtMostOnce, QosAtLeastOnce and QosExactlyOnce. An error is returned if the value is not one of these.

func (*PublishMessage) SetRemainingLength

func (this *PublishMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PublishMessage) SetResponseTopic

func (this *PublishMessage) SetResponseTopic(responseTopic []byte)

func (*PublishMessage) SetRetain

func (this *PublishMessage) SetRetain(v bool)

SetRetain sets the value of the RETAIN flag.

func (*PublishMessage) SetSubscriptionIdentifier

func (this *PublishMessage) SetSubscriptionIdentifier(subscriptionIdentifier uint32)

func (*PublishMessage) SetTopic

func (this *PublishMessage) SetTopic(v []byte) error

SetTopic sets the the topic name that identifies the information channel to which payload data is published. An error is returned if ValidTopic() is falbase.

func (*PublishMessage) SetTopicAlias

func (this *PublishMessage) SetTopicAlias(topicAlias uint16)

func (*PublishMessage) SetType

func (this *PublishMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (PublishMessage) String

func (this PublishMessage) String() string

func (*PublishMessage) SubscriptionIdentifier

func (this *PublishMessage) SubscriptionIdentifier() uint32

func (*PublishMessage) Topic

func (this *PublishMessage) Topic() []byte

Topic returns the the topic name that identifies the information channel to which payload data is published.

func (*PublishMessage) TopicAlias

func (this *PublishMessage) TopicAlias() uint16

func (*PublishMessage) Type

func (this *PublishMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*PublishMessage) UserProperty

func (this *PublishMessage) UserProperty() [][]byte

type PubrecMessage

type PubrecMessage struct {
	PubackMessage
}

func NewPubrecMessage

func NewPubrecMessage() *PubrecMessage

NewPubrecMessage creates a new PUBREC message.

func (*PubrecMessage) Decode

func (this *PubrecMessage) Decode(src []byte) (int, error)

func (*PubrecMessage) Desc

func (this *PubrecMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PubrecMessage) Flags

func (this *PubrecMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PubrecMessage) MtypeFlags

func (this *PubrecMessage) MtypeFlags() byte

func (*PubrecMessage) Name

func (this *PubrecMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PubrecMessage) PacketId

func (this *PubrecMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PubrecMessage) RemainingLength

func (this *PubrecMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PubrecMessage) SetMtypeFlags

func (this *PubrecMessage) SetMtypeFlags(mtypeflags byte)

func (*PubrecMessage) SetPacketId

func (this *PubrecMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PubrecMessage) SetRemainingLength

func (this *PubrecMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PubrecMessage) SetType

func (this *PubrecMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (*PubrecMessage) Type

func (this *PubrecMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

type PubrelMessage

type PubrelMessage struct {
	PubackMessage
}

A PUBREL Packet is the response to a PUBREC Packet. It is the third packet of the QoS 2 protocol exchange.

func NewPubrelMessage

func NewPubrelMessage() *PubrelMessage

NewPubrelMessage creates a new PUBREL message.

func (*PubrelMessage) Decode

func (this *PubrelMessage) Decode(src []byte) (int, error)

func (*PubrelMessage) Desc

func (this *PubrelMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*PubrelMessage) Flags

func (this *PubrelMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*PubrelMessage) MtypeFlags

func (this *PubrelMessage) MtypeFlags() byte

func (*PubrelMessage) Name

func (this *PubrelMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*PubrelMessage) PacketId

func (this *PubrelMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*PubrelMessage) RemainingLength

func (this *PubrelMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*PubrelMessage) SetMtypeFlags

func (this *PubrelMessage) SetMtypeFlags(mtypeflags byte)

func (*PubrelMessage) SetPacketId

func (this *PubrelMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*PubrelMessage) SetRemainingLength

func (this *PubrelMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*PubrelMessage) SetType

func (this *PubrelMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (*PubrelMessage) Type

func (this *PubrelMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

type ReasonCode

type ReasonCode uint8

原因码

const (
	Success                            ReasonCode = 0x00    // 成功 CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK, AUTH
	NormalDisconnected                            = Success // 正常断开 DISCONNECT
	Qos0                                          = Success // 授权的 QoS 0 SUBACK
	Qos1                               ReasonCode = 0x01    // 授权的 QoS 1 SUBACK
	Qos2                               ReasonCode = 0x02    // 授权的 QoS 2 SUBACK
	DisconnectionIncludesWill          ReasonCode = 0x04    // 包含遗嘱的断开 DISCONNECT
	NoMatchSubscription                ReasonCode = 0x10    // 无匹配订阅 PUBACK, PUBREC
	NoExistSubscription                ReasonCode = 0x11    // 订阅不存在 UNSUBACK
	ContinueAuthentication             ReasonCode = 0x18    // 继续认证 AUTH
	ReAuthentication                   ReasonCode = 0x19    // 重新认证 AUTH
	UnspecifiedError                   ReasonCode = 0x80    // 未指明的错误 CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT
	InvalidMessage                     ReasonCode = 0x81    // 无效报文 CONNACK, DISCONNECT
	ProtocolError                      ReasonCode = 0x82    // 协议错误 CONNACK, DISCONNECT
	ImplementError                     ReasonCode = 0x83    // 实现错误 CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT
	UnSupportedProtocolVersion         ReasonCode = 0x84    // 协议版本不支持 CONNACK
	CustomerIdentifierInvalid          ReasonCode = 0x85    // 客户标识符无效 CONNACK
	UserNameOrPasswordIsIncorrect      ReasonCode = 0x86    // 用户名密码错误 CONNACK
	UnAuthorized                       ReasonCode = 0x87    // 未授权 CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT
	ServerUnavailable                  ReasonCode = 0x88    // 服务端不可用 CONNACK
	ServiceBusy                        ReasonCode = 0x89    // 服务端正忙 CONNACK, DISCONNECT
	Forbid                             ReasonCode = 0x8A    // 禁止 CONNACK
	ServerBeingShutDown                ReasonCode = 0x8B    // 服务端关闭中 DISCONNECT
	InvalidAuthenticationMethod        ReasonCode = 0x8C    // 无效的认证方法 CONNACK, DISCONNECT
	KeepAliveTimeout                   ReasonCode = 0x8D    // 保活超时 DISCONNECT
	SessionTakeover                    ReasonCode = 0x8E    // 会话被接管 DISCONNECT
	InvalidTopicFilter                 ReasonCode = 0x8F    // 主题过滤器无效 SUBACK, UNSUBACK, DISCONNECT
	InvalidTopicName                   ReasonCode = 0x90    // 主题名无效 CONNACK, PUBACK, PUBREC, DISCONNECT
	PacketIdentifierIsOccupied         ReasonCode = 0x91    // 报文标识符已被占用 PUBACK, PUBREC, SUBACK, UNSUBACK
	PacketIdentifierInvalid            ReasonCode = 0x92    // 报文标识符无效 PUBREL, PUBCOMP
	MaximumNumberReceivedExceeded      ReasonCode = 0x93    // 接收超出最大数量 DISCONNECT
	InvalidTopicAlias                  ReasonCode = 0x94    // 主题别名无效 DISCONNECT
	MessageTooLong                     ReasonCode = 0x95    // 报文过长 CONNACK, DISCONNECT
	TooManyMessages                    ReasonCode = 0x96    // 消息太过频繁 DISCONNECT
	BeyondQuota                        ReasonCode = 0x97    // 超出配额 CONNACK, PUBACK, PUBREC, SUBACK, DISCONNECT
	ManagementBehavior                 ReasonCode = 0x98    // 管理行为 DISCONNECT
	InvalidLoadFormat                  ReasonCode = 0x99    // 载荷格式无效 CONNACK, PUBACK, PUBREC, DISCONNECT
	UnsupportedRetention               ReasonCode = 0x9A    // 不支持保留 CONNACK, DISCONNECT
	UnsupportedQoSLevel                ReasonCode = 0x9B    // 不支持的 QoS 等级 CONNACK, DISCONNECT
	UseOtherServers                    ReasonCode = 0x9C    //(临时)使用其他服务端 CONNACK, DISCONNECT
	ServerHasMoved                     ReasonCode = 0x9D    // 服务端已(永久)移动 CONNACK, DISCONNECT
	UnsupportedSharedSubscriptions     ReasonCode = 0x9E    // 不支持共享订阅 SUBACK, DISCONNECT
	ExceededConnectionRateLimit        ReasonCode = 0x9F    // 超出连接速率限制 CONNACK, DISCONNECT
	MaximumConnectionTime              ReasonCode = 0xA0    // 最大连接时间 DISCONNECT
	UnsupportedSubscriptionIdentifiers ReasonCode = 0xA1    // 不支持订阅标识符 SUBACK, DISCONNECT
	UnsupportedWildcardSubscriptions   ReasonCode = 0xA2    // 不支持通配符订阅 SUBACK, DISCONNECT
)

func (ReasonCode) Desc

func (this ReasonCode) Desc() string

Desc returns the description of the ReasonCode

func (ReasonCode) Error

func (this ReasonCode) Error() string

Error returns the corresonding error string for the ReasonCode

func (ReasonCode) String

func (this ReasonCode) String() string

func (ReasonCode) Valid

func (this ReasonCode) Valid() bool

Valid checks to see if the ReasonCode is valid. Currently valid codes are <= 5

func (ReasonCode) Value

func (this ReasonCode) Value() byte

Value returns the value of the ReasonCode, which is just the byte representation

type RetainHandling

type RetainHandling byte
const (
	// 0 = 订阅建立时发送保留消息
	CanSendRetain RetainHandling = iota
	// 1 = 订阅建立时,若该订阅当前不存在则发送保留消息
	NoExistSubSendRetain
	// 2 = 订阅建立时不要发送保留消息
	NoSendRetain
)

type SubackMessage

type SubackMessage struct {
	// contains filtered or unexported fields
}

SubackMessage A SUBACK Packet is sent by the Server to the Client to confirm receipt and processing of a SUBSCRIBE Packet.

A SUBACK Packet contains a list of return codes, that specify the maximum QoS level that was granted in each Subscription that was requested by the SUBSCRIBE.

func NewSubackMessage

func NewSubackMessage() *SubackMessage

NewSubackMessage creates a new SUBACK message.

func (*SubackMessage) AddReasonCode

func (this *SubackMessage) AddReasonCode(ret byte) error

AddReturnCode adds a single QoS return value.

func (*SubackMessage) AddReasonCodes

func (this *SubackMessage) AddReasonCodes(ret []byte) error

AddreasonCodes sets the list of QoS returns from the subscriptions sent in the SUBSCRIBE message. An error is returned if any of the QoS values are not valid.

func (*SubackMessage) AddUserProperty

func (this *SubackMessage) AddUserProperty(userProperty []byte)

func (*SubackMessage) AddUserPropertys

func (this *SubackMessage) AddUserPropertys(userProperty [][]byte)

func (*SubackMessage) Decode

func (this *SubackMessage) Decode(src []byte) (int, error)

func (*SubackMessage) Desc

func (this *SubackMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*SubackMessage) Encode

func (this *SubackMessage) Encode(dst []byte) (int, error)

func (*SubackMessage) Flags

func (this *SubackMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*SubackMessage) Len

func (this *SubackMessage) Len() int

func (*SubackMessage) MtypeFlags

func (this *SubackMessage) MtypeFlags() byte

func (*SubackMessage) Name

func (this *SubackMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*SubackMessage) PacketId

func (this *SubackMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*SubackMessage) PropertiesLen

func (this *SubackMessage) PropertiesLen() uint32

func (*SubackMessage) ReasonCodes

func (this *SubackMessage) ReasonCodes() []byte

ReasonCodes returns the list of QoS returns from the subscriptions sent in the SUBSCRIBE message.

func (*SubackMessage) ReasonStr

func (this *SubackMessage) ReasonStr() []byte

func (*SubackMessage) RemainingLength

func (this *SubackMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*SubackMessage) SetMtypeFlags

func (this *SubackMessage) SetMtypeFlags(mtypeflags byte)

func (*SubackMessage) SetPacketId

func (this *SubackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*SubackMessage) SetPropertiesLen

func (this *SubackMessage) SetPropertiesLen(propertiesLen uint32)

func (*SubackMessage) SetReasonStr

func (this *SubackMessage) SetReasonStr(reasonStr []byte)

func (*SubackMessage) SetRemainingLength

func (this *SubackMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*SubackMessage) SetType

func (this *SubackMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (SubackMessage) String

func (this SubackMessage) String() string

String returns a string representation of the message.

func (*SubackMessage) Type

func (this *SubackMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*SubackMessage) UserProperty

func (this *SubackMessage) UserProperty() [][]byte

type SubscribeMessage

type SubscribeMessage struct {
	// contains filtered or unexported fields
}

SubscribeMessage The SUBSCRIBE Packet is sent from the Client to the Server to create one or more Subscriptions. Each Subscription registers a Client’s interest in one or more Topics. The Server sends PUBLISH Packets to the Client in order to forward Application Messages that were published to Topics that match these Subscriptions. The SUBSCRIBE Packet also specifies (for each Subscription) the maximum QoS with which the Server can send Application Messages to the Client.

func NewSubscribeMessage

func NewSubscribeMessage() *SubscribeMessage

NewSubscribeMessage creates a new SUBSCRIBE message.

func (*SubscribeMessage) AddTopic

func (this *SubscribeMessage) AddTopic(topic []byte, qos byte) error

AddTopic adds a single topic to the message, along with the corresponding QoS. An error is returned if QoS is invalid.

func (*SubscribeMessage) AddTopicAll

func (this *SubscribeMessage) AddTopicAll(topic []byte, qos byte, local, retainAsPub bool, retainHandling byte) error

func (*SubscribeMessage) AddUserProperty

func (this *SubscribeMessage) AddUserProperty(userProperty []byte)

func (*SubscribeMessage) AddUserPropertys

func (this *SubscribeMessage) AddUserPropertys(userProperty [][]byte)

func (*SubscribeMessage) Decode

func (this *SubscribeMessage) Decode(src []byte) (int, error)

func (*SubscribeMessage) Desc

func (this *SubscribeMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*SubscribeMessage) Encode

func (this *SubscribeMessage) Encode(dst []byte) (int, error)

func (*SubscribeMessage) Flags

func (this *SubscribeMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*SubscribeMessage) Len

func (this *SubscribeMessage) Len() int

func (*SubscribeMessage) MtypeFlags

func (this *SubscribeMessage) MtypeFlags() byte

func (*SubscribeMessage) Name

func (this *SubscribeMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*SubscribeMessage) PacketId

func (this *SubscribeMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*SubscribeMessage) PropertiesLen

func (this *SubscribeMessage) PropertiesLen() uint32

func (*SubscribeMessage) Qos

func (this *SubscribeMessage) Qos() []byte

Qos returns the list of QoS current in the message.

func (*SubscribeMessage) RemainingLength

func (this *SubscribeMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*SubscribeMessage) RemoveTopic

func (this *SubscribeMessage) RemoveTopic(topic []byte)

RemoveTopic removes a single topic from the list of existing ones in the message. If topic does not exist it just does nothing.

func (*SubscribeMessage) SetMtypeFlags

func (this *SubscribeMessage) SetMtypeFlags(mtypeflags byte)

func (*SubscribeMessage) SetPacketId

func (this *SubscribeMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*SubscribeMessage) SetPropertiesLen

func (this *SubscribeMessage) SetPropertiesLen(propertiesLen uint32)

func (*SubscribeMessage) SetRemainingLength

func (this *SubscribeMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*SubscribeMessage) SetSubscriptionIdentifier

func (this *SubscribeMessage) SetSubscriptionIdentifier(subscriptionIdentifier uint32)

func (*SubscribeMessage) SetTopicLocal

func (this *SubscribeMessage) SetTopicLocal(topic []byte, local bool)

func (*SubscribeMessage) SetTopicRetainAsPublished

func (this *SubscribeMessage) SetTopicRetainAsPublished(topic []byte, rap bool)

func (*SubscribeMessage) SetTopicRetainHandling

func (this *SubscribeMessage) SetTopicRetainHandling(topic []byte, hand RetainHandling)

func (*SubscribeMessage) SetType

func (this *SubscribeMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (SubscribeMessage) String

func (this SubscribeMessage) String() string

func (*SubscribeMessage) SubscriptionIdentifier

func (this *SubscribeMessage) SubscriptionIdentifier() uint32

func (*SubscribeMessage) TopicExists

func (this *SubscribeMessage) TopicExists(topic []byte) bool

TopicExists checks to see if a topic exists in the list.

func (*SubscribeMessage) TopicLocal

func (this *SubscribeMessage) TopicLocal(topic []byte) bool

func (*SubscribeMessage) TopicQos

func (this *SubscribeMessage) TopicQos(topic []byte) byte

TopicQos returns the QoS level of a topic. If topic does not exist, QosFailure is returned.

func (*SubscribeMessage) TopicRetainAsPublished

func (this *SubscribeMessage) TopicRetainAsPublished(topic []byte) bool

func (*SubscribeMessage) TopicRetainHandling

func (this *SubscribeMessage) TopicRetainHandling(topic []byte) RetainHandling

func (*SubscribeMessage) Topics

func (this *SubscribeMessage) Topics() [][]byte

Topics returns a list of topics sent by the Client.

func (*SubscribeMessage) Type

func (this *SubscribeMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*SubscribeMessage) UserProperty

func (this *SubscribeMessage) UserProperty() [][]byte

type UnsubackMessage

type UnsubackMessage struct {
	// contains filtered or unexported fields
}

UnsubackMessage The UNSUBACK Packet is sent by the Server to the Client to confirm receipt of an UNSUBSCRIBE Packet.

func NewUnsubackMessage

func NewUnsubackMessage() *UnsubackMessage

NewUnsubackMessage creates a new UNSUBACK message.

func (*UnsubackMessage) AddReasonCode

func (u *UnsubackMessage) AddReasonCode(reasonCode byte)

func (*UnsubackMessage) AddReasonCodes

func (u *UnsubackMessage) AddReasonCodes(reasonCodes []byte)

func (*UnsubackMessage) AddUserProperty

func (this *UnsubackMessage) AddUserProperty(userProperty []byte)

func (*UnsubackMessage) AddUserPropertys

func (this *UnsubackMessage) AddUserPropertys(userProperty [][]byte)

func (*UnsubackMessage) Decode

func (this *UnsubackMessage) Decode(src []byte) (int, error)

func (*UnsubackMessage) Desc

func (this *UnsubackMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*UnsubackMessage) Encode

func (this *UnsubackMessage) Encode(dst []byte) (int, error)

func (*UnsubackMessage) Flags

func (this *UnsubackMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*UnsubackMessage) Len

func (this *UnsubackMessage) Len() int

func (*UnsubackMessage) MtypeFlags

func (this *UnsubackMessage) MtypeFlags() byte

func (*UnsubackMessage) Name

func (this *UnsubackMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*UnsubackMessage) PacketId

func (this *UnsubackMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*UnsubackMessage) PropertyLen

func (u *UnsubackMessage) PropertyLen() uint32

func (*UnsubackMessage) ReasonCodes

func (u *UnsubackMessage) ReasonCodes() []byte

func (*UnsubackMessage) ReasonStr

func (u *UnsubackMessage) ReasonStr() []byte

func (*UnsubackMessage) RemainingLength

func (this *UnsubackMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*UnsubackMessage) SetMtypeFlags

func (this *UnsubackMessage) SetMtypeFlags(mtypeflags byte)

func (*UnsubackMessage) SetPacketId

func (this *UnsubackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*UnsubackMessage) SetPropertyLen

func (u *UnsubackMessage) SetPropertyLen(propertyLen uint32)

func (*UnsubackMessage) SetReasonStr

func (u *UnsubackMessage) SetReasonStr(reasonStr []byte)

func (*UnsubackMessage) SetRemainingLength

func (this *UnsubackMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*UnsubackMessage) SetType

func (this *UnsubackMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (UnsubackMessage) String

func (this UnsubackMessage) String() string

func (*UnsubackMessage) Type

func (this *UnsubackMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*UnsubackMessage) UserProperty

func (u *UnsubackMessage) UserProperty() [][]byte

type UnsubscribeMessage

type UnsubscribeMessage struct {
	// contains filtered or unexported fields
}

An UNSUBSCRIBE Packet is sent by the Client to the Server, to unsubscribe from topics.

func NewUnsubscribeMessage

func NewUnsubscribeMessage() *UnsubscribeMessage

NewUnsubscribeMessage creates a new UNSUBSCRIBE message.

func (*UnsubscribeMessage) AddTopic

func (this *UnsubscribeMessage) AddTopic(topic []byte)

AddTopic adds a single topic to the message.

func (*UnsubscribeMessage) AddUserProperty

func (this *UnsubscribeMessage) AddUserProperty(userProperty []byte)

func (*UnsubscribeMessage) AddUserPropertys

func (this *UnsubscribeMessage) AddUserPropertys(userProperty [][]byte)

func (*UnsubscribeMessage) Decode

func (this *UnsubscribeMessage) Decode(src []byte) (int, error)

Decode reads from the io.Reader parameter until a full message is decoded, or when io.Reader returns EOF or error. The first return value is the number of bytes read from io.Reader. The second is error if Decode encounters any problems.

func (*UnsubscribeMessage) Desc

func (this *UnsubscribeMessage) Desc() string

Desc returns a string description of the message type. For example, a CONNECT message would return "Client request to connect to Server." These descriptions are statically defined (copied from the MQTT spec) and cannot be changed.

func (*UnsubscribeMessage) Encode

func (this *UnsubscribeMessage) Encode(dst []byte) (int, error)

Encode returns an io.Reader in which the encoded bytes can be read. The second return value is the number of bytes encoded, so the caller knows how many bytes there will be. If Encode returns an error, then the first two return values should be considered invalid. Any changes to the message after Encode() is called will invalidate the io.Reader.

func (*UnsubscribeMessage) Flags

func (this *UnsubscribeMessage) Flags() byte

Flags returns the fixed header flags for this message.

func (*UnsubscribeMessage) Len

func (this *UnsubscribeMessage) Len() int

func (*UnsubscribeMessage) MtypeFlags

func (this *UnsubscribeMessage) MtypeFlags() byte

func (*UnsubscribeMessage) Name

func (this *UnsubscribeMessage) Name() string

Name returns a string representation of the message type. Examples include "PUBLISH", "SUBSCRIBE", and others. This is statically defined for each of the message types and cannot be changed.

func (*UnsubscribeMessage) PacketId

func (this *UnsubscribeMessage) PacketId() uint16

PacketId returns the ID of the packet.

func (*UnsubscribeMessage) PropertyLen

func (this *UnsubscribeMessage) PropertyLen() uint32

func (*UnsubscribeMessage) RemainingLength

func (this *UnsubscribeMessage) RemainingLength() int32

RemainingLength returns the length of the non-fixed-header part of the message.

func (*UnsubscribeMessage) RemoveTopic

func (this *UnsubscribeMessage) RemoveTopic(topic []byte)

RemoveTopic removes a single topic from the list of existing ones in the message. If topic does not exist it just does nothing.

func (*UnsubscribeMessage) SetMtypeFlags

func (this *UnsubscribeMessage) SetMtypeFlags(mtypeflags byte)

func (*UnsubscribeMessage) SetPacketId

func (this *UnsubscribeMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

func (*UnsubscribeMessage) SetPropertyLen

func (this *UnsubscribeMessage) SetPropertyLen(propertyLen uint32)

func (*UnsubscribeMessage) SetRemainingLength

func (this *UnsubscribeMessage) SetRemainingLength(remlen int32) error

SetRemainingLength sets the length of the non-fixed-header part of the message. It returns error if the length is greater than 268435455, which is the max message length as defined by the MQTT spec.

func (*UnsubscribeMessage) SetType

func (this *UnsubscribeMessage) SetType(mtype MessageType) error

SetType sets the message type of this message. It also correctly sets the default flags for the message type. It returns an error if the type is invalid.

func (UnsubscribeMessage) String

func (this UnsubscribeMessage) String() string

func (*UnsubscribeMessage) TopicExists

func (this *UnsubscribeMessage) TopicExists(topic []byte) bool

TopicExists checks to see if a topic exists in the list.

func (*UnsubscribeMessage) Topics

func (this *UnsubscribeMessage) Topics() [][]byte

Topics returns a list of topics sent by the Client.

func (*UnsubscribeMessage) Type

func (this *UnsubscribeMessage) Type() MessageType

Type returns the MessageType of the Message. The retured value should be one of the constants defined for MessageType.

func (*UnsubscribeMessage) UserProperty

func (this *UnsubscribeMessage) UserProperty() [][]byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL