message

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, Apache-2.0 Imports: 5 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",
}

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

Functions

func ValidConnackError

func ValidConnackError(err error) bool

ValidConnackError checks to see if the error is a Connack Error or not ValidConnackError检查该错误是否是Connack错误

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 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 ValidVersion

func ValidVersion(v byte) bool

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

Types

type ConnackCode

type ConnackCode byte

ConnackCode is the type representing the return code in the CONNACK message, returned after the initial CONNECT message

const (
	// Connection accepted
	ConnectionAccepted ConnackCode = iota

	// The Server does not support the level of the MQTT protocol requested by the Client
	ErrInvalidProtocolVersion

	// The Client identifier is correct UTF-8 but not allowed by the server
	ErrIdentifierRejected

	// The Network Connection has been made but the MQTT service is unavailable
	ErrServerUnavailable

	// The data in the user name or password is malformed
	ErrBadUsernameOrPassword

	// The Client is not authorized to connect
	ErrNotAuthorized
)

func (ConnackCode) Desc

func (this ConnackCode) Desc() string

Desc returns the description of the ConnackCode

func (ConnackCode) Error

func (this ConnackCode) Error() string

Error returns the corresonding error string for the ConnackCode

func (ConnackCode) Valid

func (this ConnackCode) Valid() bool

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

func (ConnackCode) Value

func (this ConnackCode) Value() byte

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

type ConnackMessage

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

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) 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) 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) RemainingLength

func (this *ConnackMessage) RemainingLength() int32

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

func (*ConnackMessage) ReturnCode

func (this *ConnackMessage) ReturnCode() ConnackCode

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

func (*ConnackMessage) SessionPresent

func (this *ConnackMessage) SessionPresent() bool

SessionPresent returns the session present flag value

func (*ConnackMessage) SetPacketId

func (this *ConnackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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) SetReturnCode

func (this *ConnackMessage) SetReturnCode(ret ConnackCode)

func (*ConnackMessage) SetSessionPresent

func (this *ConnackMessage) SetSessionPresent(v bool)

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

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) String

func (this ConnackMessage) String() string

String returns a string representation of the CONNACK message

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.

type ConnectMessage

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

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) 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) 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) Decode

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

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) 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) RemainingLength

func (this *ConnectMessage) RemainingLength() int32

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

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) SetKeepAlive

func (this *ConnectMessage) SetKeepAlive(v uint16)

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

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) 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) 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) 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) 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) 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) 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) 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) 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.

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) 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) 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) RemainingLength

func (this *DisconnectMessage) RemainingLength() int32

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

func (*DisconnectMessage) SetPacketId

func (this *DisconnectMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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) 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 (this DisconnectMessage) String() string

String returns a string representation of the message.

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.

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

	// 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 {
	DisconnectMessage
}

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) 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) 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) 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) 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 {
	DisconnectMessage
}

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) 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) 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) 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) 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 PubackMessage

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

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) 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) 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) RemainingLength

func (this *PubackMessage) RemainingLength() int32

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

func (*PubackMessage) SetPacketId

func (this *PubackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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.

type PubcompMessage

type PubcompMessage struct {
	PubackMessage
}

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) 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) 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) 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
}

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) 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) 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) 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) 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) SetDup

func (this *PublishMessage) SetDup(v bool)

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

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) 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) SetRetain

func (this *PublishMessage) SetRetain(v bool)

SetRetain sets the value of the RETAIN flag.

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) 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) 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) 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.

type PubrecMessage

type PubrecMessage struct {
	PubackMessage
}

func NewPubrecMessage

func NewPubrecMessage() *PubrecMessage

NewPubrecMessage creates a new PUBREC message.

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) 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) 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) 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) 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) 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 SubackMessage

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

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) AddReturnCode

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

AddReturnCode adds a single QoS return value.

func (*SubackMessage) AddReturnCodes

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

AddReturnCodes 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) 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) 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) RemainingLength

func (this *SubackMessage) RemainingLength() int32

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

func (*SubackMessage) ReturnCodes

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

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

func (*SubackMessage) SetPacketId

func (this *SubackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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.

type SubscribeMessage

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

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) 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) 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) 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) SetPacketId

func (this *SubscribeMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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) 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) TopicExists

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

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

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) 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.

type UnsubackMessage

type UnsubackMessage struct {
	PubackMessage
}

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) 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) Flags

func (this *UnsubackMessage) Flags() byte

Flags returns the fixed header flags for this message.

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) RemainingLength

func (this *UnsubackMessage) RemainingLength() int32

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

func (*UnsubackMessage) SetPacketId

func (this *UnsubackMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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) 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.

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) 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) 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) 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) SetPacketId

func (this *UnsubscribeMessage) SetPacketId(v uint16)

SetPacketId sets the ID of the packet.

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.

Jump to

Keyboard shortcuts

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