Documentation ¶
Overview ¶
Package mq provides a mqtt-v5.0 protocol implementation
The specification is found at https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
Example (PacketReadWrite) ¶
package main import ( "bytes" "fmt" "github.com/gregoryv/mq" ) func main() { var buf bytes.Buffer { // create and write packet p := mq.NewConnect() p.SetClientID("pink") p.SetUsername("gopher") p.SetPassword([]byte("cute")) p.SetWill(mq.Pub(1, "client/gone", "pink"), 7) p.WriteTo(&buf) } { // read the packet p, _ := mq.ReadPacket(&buf) fmt.Print(p) } }
Output: CONNECT ---- up--1w-- MQTT5 pink 0s 58 bytes
Index ¶
- Constants
- Variables
- func Dump(w io.Writer, p Packet)
- type Auth
- func (p *Auth) AuthData() []byte
- func (p *Auth) AuthMethod() string
- func (p *Auth) ReasonCode() ReasonCode
- func (p *Auth) ReasonString() string
- func (p *Auth) SetAuthData(v []byte)
- func (p *Auth) SetAuthMethod(v string)
- func (p *Auth) SetReasonCode(v ReasonCode)
- func (p *Auth) SetReasonString(v string)
- func (p *Auth) String() string
- func (p *Auth) UnmarshalBinary(data []byte) error
- func (p *Auth) WriteTo(w io.Writer) (int64, error)
- type ConnAck
- func (p *ConnAck) AssignedClientID() string
- func (p *ConnAck) AuthData() []byte
- func (p *ConnAck) AuthMethod() string
- func (p *ConnAck) HasFlag(v byte) bool
- func (p *ConnAck) MaxPacketSize() uint32
- func (p *ConnAck) MaxQoS() uint8
- func (p *ConnAck) ReasonCode() ReasonCode
- func (p *ConnAck) ReasonString() string
- func (p *ConnAck) ReceiveMax() uint16
- func (p *ConnAck) ResponseInformation() string
- func (p *ConnAck) RetainAvailable() bool
- func (p *ConnAck) ServerKeepAlive() uint16
- func (p *ConnAck) ServerReference() string
- func (p *ConnAck) SessionExpiryInterval() uint32
- func (p *ConnAck) SessionPresent() bool
- func (p *ConnAck) SetAssignedClientID(v string)
- func (p *ConnAck) SetAuthData(v []byte)
- func (p *ConnAck) SetAuthMethod(v string)
- func (p *ConnAck) SetMaxPacketSize(v uint32)
- func (p *ConnAck) SetMaxQoS(v uint8)
- func (p *ConnAck) SetReasonCode(v ReasonCode)
- func (p *ConnAck) SetReasonString(v string)
- func (p *ConnAck) SetReceiveMax(v uint16)
- func (p *ConnAck) SetResponseInformation(v string)
- func (p *ConnAck) SetRetainAvailable(v bool)
- func (p *ConnAck) SetServerKeepAlive(v uint16)
- func (p *ConnAck) SetServerReference(v string)
- func (p *ConnAck) SetSessionExpiryInterval(v uint32)
- func (p *ConnAck) SetSessionPresent(v bool)
- func (p *ConnAck) SetSharedSubAvailable(v bool)
- func (p *ConnAck) SetSubIdentifiersAvailable(v bool)
- func (p *ConnAck) SetTopicAliasMax(v uint16)
- func (p *ConnAck) SetWildcardSubAvailable(v bool)
- func (p *ConnAck) SharedSubAvailable() bool
- func (p *ConnAck) String() string
- func (p *ConnAck) SubIdentifiersAvailable() bool
- func (p *ConnAck) TopicAliasMax() uint16
- func (p *ConnAck) UnmarshalBinary(data []byte) error
- func (p *ConnAck) WildcardSubAvailable() bool
- func (p *ConnAck) WriteTo(w io.Writer) (int64, error)
- type Connect
- func (p *Connect) AuthData() []byte
- func (p *Connect) AuthMethod() string
- func (p *Connect) CleanStart() bool
- func (p *Connect) ClientID() string
- func (p *Connect) HasFlag(v byte) bool
- func (p *Connect) KeepAlive() uint16
- func (p *Connect) MaxPacketSize() uint32
- func (p *Connect) Password() []byte
- func (p *Connect) ProtocolName() string
- func (p *Connect) ProtocolVersion() uint8
- func (p *Connect) ReceiveMax() uint16
- func (p *Connect) RequestProblemInfo() bool
- func (p *Connect) RequestResponseInfo() bool
- func (p *Connect) SessionExpiryInterval() uint32
- func (p *Connect) SetAuthData(v []byte)
- func (p *Connect) SetAuthMethod(v string)
- func (p *Connect) SetCleanStart(v bool)
- func (p *Connect) SetClientID(v string)
- func (p *Connect) SetKeepAlive(v uint16)
- func (p *Connect) SetMaxPacketSize(v uint32)
- func (p *Connect) SetPassword(v []byte)
- func (p *Connect) SetProtocolName(v string)
- func (p *Connect) SetProtocolVersion(v uint8)
- func (p *Connect) SetReceiveMax(v uint16)
- func (p *Connect) SetRequestProblemInfo(v bool)
- func (p *Connect) SetRequestResponseInfo(v bool)
- func (p *Connect) SetSessionExpiryInterval(v uint32)
- func (p *Connect) SetTopicAliasMax(v uint16)
- func (p *Connect) SetUsername(v string)
- func (p *Connect) SetWill(will *Publish, delayInterval uint32)
- func (p *Connect) String() string
- func (p *Connect) TopicAliasMax() uint16
- func (p *Connect) UnmarshalBinary(data []byte) error
- func (p *Connect) Username() string
- func (p *Connect) Will() *Publish
- func (p *Connect) WriteTo(w io.Writer) (int64, error)
- type ControlPacket
- type Disconnect
- type FilterOption
- type HasPacketID
- type HasReason
- type HasWellFormed
- type Ident
- type Malformed
- type Opt
- type Packet
- type PingReq
- type PingResp
- type PubAck
- func (p *PubAck) PacketID() uint16
- func (p *PubAck) ReasonCode() ReasonCode
- func (p *PubAck) ReasonString() string
- func (p *PubAck) SetPacketID(v uint16)
- func (p *PubAck) SetReasonCode(v ReasonCode)
- func (p *PubAck) SetReasonString(v string)
- func (p *PubAck) String() string
- func (p *PubAck) UnmarshalBinary(data []byte) error
- func (p *PubAck) WriteTo(w io.Writer) (int64, error)
- type PubComp
- func (p *PubComp) PacketID() uint16
- func (p *PubComp) ReasonCode() ReasonCode
- func (p *PubComp) ReasonString() string
- func (p *PubComp) SetPacketID(v uint16)
- func (p *PubComp) SetReasonCode(v ReasonCode)
- func (p *PubComp) SetReasonString(v string)
- func (p *PubComp) String() string
- func (p *PubComp) UnmarshalBinary(data []byte) error
- func (p *PubComp) WriteTo(w io.Writer) (int64, error)
- type PubRec
- func (p *PubRec) PacketID() uint16
- func (p *PubRec) ReasonCode() ReasonCode
- func (p *PubRec) ReasonString() string
- func (p *PubRec) SetPacketID(v uint16)
- func (p *PubRec) SetReasonCode(v ReasonCode)
- func (p *PubRec) SetReasonString(v string)
- func (p *PubRec) String() string
- func (p *PubRec) UnmarshalBinary(data []byte) error
- func (p *PubRec) WriteTo(w io.Writer) (int64, error)
- type PubRel
- func (p *PubRel) PacketID() uint16
- func (p *PubRel) ReasonCode() ReasonCode
- func (p *PubRel) ReasonString() string
- func (p *PubRel) SetPacketID(v uint16)
- func (p *PubRel) SetReasonCode(v ReasonCode)
- func (p *PubRel) SetReasonString(v string)
- func (p *PubRel) String() string
- func (p *PubRel) UnmarshalBinary(data []byte) error
- func (p *PubRel) WriteTo(w io.Writer) (int64, error)
- type Publish
- func (p *Publish) AddSubscriptionID(v uint32)
- func (p *Publish) ContentType() string
- func (p *Publish) CorrelationData() []byte
- func (p *Publish) Duplicate() bool
- func (p *Publish) MessageExpiryInterval() uint32
- func (p *Publish) PacketID() uint16
- func (p *Publish) Payload() []byte
- func (p *Publish) PayloadFormat() bool
- func (p *Publish) QoS() uint8
- func (p *Publish) ResponseTopic() string
- func (p *Publish) Retain() bool
- func (p *Publish) SetContentType(v string)
- func (p *Publish) SetCorrelationData(v []byte)
- func (p *Publish) SetDuplicate(v bool)
- func (p *Publish) SetMessageExpiryInterval(v uint32)
- func (p *Publish) SetPacketID(v uint16)
- func (p *Publish) SetPayload(v []byte)
- func (p *Publish) SetPayloadFormat(v bool)
- func (p *Publish) SetQoS(v uint8)
- func (p *Publish) SetResponseTopic(v string)
- func (p *Publish) SetRetain(v bool)
- func (p *Publish) SetTopicAlias(v uint16)
- func (p *Publish) SetTopicName(v string)
- func (p *Publish) String() string
- func (p *Publish) SubscriptionIDs() []uint32
- func (p *Publish) TopicAlias() uint16
- func (p *Publish) TopicName() string
- func (p *Publish) UnmarshalBinary(data []byte) error
- func (p *Publish) WellFormed() *Malformed
- func (p *Publish) WriteTo(w io.Writer) (int64, error)
- type ReasonCode
- type SubAck
- func (p *SubAck) AddReasonCode(v ReasonCode)
- func (p *SubAck) PacketID() uint16
- func (p *SubAck) ReasonCodes() []uint8
- func (p *SubAck) ReasonString() string
- func (p *SubAck) SetPacketID(v uint16)
- func (p *SubAck) SetReasonString(v string)
- func (p *SubAck) String() string
- func (p *SubAck) UnmarshalBinary(data []byte) error
- func (p *SubAck) WriteTo(w io.Writer) (int64, error)
- type Subscribe
- func (p *Subscribe) AddFilters(v ...TopicFilter)
- func (p *Subscribe) Filters() []TopicFilter
- func (p *Subscribe) PacketID() uint16
- func (p *Subscribe) SetPacketID(v uint16)
- func (p *Subscribe) SetSubscriptionID(v int)
- func (p *Subscribe) String() string
- func (p *Subscribe) SubscriptionID() int
- func (p *Subscribe) UnmarshalBinary(data []byte) error
- func (p *Subscribe) WellFormed() *Malformed
- func (p *Subscribe) WriteTo(w io.Writer) (int64, error)
- type TopicFilter
- type Undefined
- type UnsubAck
- func (p *UnsubAck) AddReasonCode(v ReasonCode)
- func (p *UnsubAck) PacketID() uint16
- func (p *UnsubAck) ReasonCodes() []uint8
- func (p *UnsubAck) ReasonString() string
- func (p *UnsubAck) SetPacketID(v uint16)
- func (p *UnsubAck) SetReasonString(v string)
- func (p *UnsubAck) String() string
- func (p *UnsubAck) UnmarshalBinary(data []byte) error
- func (p *UnsubAck) WriteTo(w io.Writer) (int64, error)
- type Unsubscribe
- type UserProp
- type UserProperties
Examples ¶
- Package (PacketReadWrite)
- Auth.String
- ConnAck
- ConnAck (WithReasonCode)
- ConnAck.String
- Connect
- Connect.String
- Disconnect.String
- Dump (Unsubscribe)
- Malformed.Error
- NewPubComp
- NewPubRec
- NewPubRel
- PingReq.String
- PingResp.String
- PubAck.String
- Publish (StringMalformed)
- Publish.String
- SubAck.String
- Subscribe (Malformed)
- Subscribe.String
- TopicFilter.String
- UserProperties.AddUserProp
Constants ¶
const ( Reserved byte = 1 << iota CleanStart WillFlag WillQoS1 WillQoS2 WillRetain PasswordFlag UsernameFlag )
CONNECT flags used in Connect.HasFlag()
const ( UNDEFINED byte = (iota << 4) // 0 Forbidden Reserved CONNECT // 1 Client to Server Connection request CONNACK // 2 Server to Client Connect acknowledgment PUBLISH // 3 Client to Server or Publish message PUBACK // 4 Client to Server or Publish acknowledgment (QoS 1) PUBREC // 5 Client to Server or Publish received (QoS 2 delivery part 1) PUBREL // 6 Client to Server or Publish release (QoS 2 delivery part 2) PUBCOMP // 7 Client to Server or Publish complete (QoS 2 delivery part 3) SUBSCRIBE // 8 Client to Server Subscribe request SUBACK // 9 Server to Client Subscribe acknowledgment UNSUBSCRIBE // 10 Client to Server Unsubscribe request UNSUBACK // 11 Server to Client Unsubscribe acknowledgment PINGREQ // 12 Client to Server PING request PINGRESP // 13 Server to Client PING response DISCONNECT // 14 Client to Server or Disconnect notification AUTH // 15 Client to Server or Server to Client Authentication exchange )
2.1.2 MQTT Control Packet type
https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_MQTT_Control_Packet
const ( RETAIN byte = 0b0000_0001 QoS1 byte = 0b0000_0010 QoS2 byte = 0b0000_0100 QoS3 byte = 0b0000_0110 // malformed! DUP byte = 0b0000_1000 )
firstByte header flags
const (
SessionPresent uint8 = 1
)
Variables ¶
var ErrMissingData = fmt.Errorf("missing data")
Functions ¶
func Dump ¶ added in v0.24.0
Dump writes all packet fields to the given writer, including empty value ones.
Example (Unsubscribe) ¶
s := NewUnsubscribe() s.AddUserProp("color", "purple") s.AddFilter("a/b/c") s.AddFilter("d/e") Dump(os.Stdout, s)
Output: PacketID: 0 Filters 0. a/b/c 1. d/e UserProperties 0. color: "purple"
Types ¶
type Auth ¶
type Auth struct { UserProperties // contains filtered or unexported fields }
func (*Auth) AuthMethod ¶ added in v0.24.0
func (*Auth) ReasonCode ¶
func (p *Auth) ReasonCode() ReasonCode
func (*Auth) ReasonString ¶ added in v0.24.0
func (*Auth) SetAuthData ¶ added in v0.24.0
func (*Auth) SetAuthMethod ¶ added in v0.24.0
func (*Auth) SetReasonCode ¶
func (p *Auth) SetReasonCode(v ReasonCode)
func (*Auth) SetReasonString ¶ added in v0.24.0
func (*Auth) String ¶
Example ¶
p := NewAuth() fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: AUTH ---- 2 bytes 3210 Size 3-0 reserved
func (*Auth) UnmarshalBinary ¶
type ConnAck ¶
type ConnAck struct { UserProperties // contains filtered or unexported fields }
Example ¶
a := NewConnAck() a.SetSessionPresent(true) fmt.Print(a.String())
Output: CONNACK ---- -------s 5 bytes
Example (WithReasonCode) ¶
a := NewConnAck() a.SetSessionPresent(true) a.SetReasonCode(NotAuthorized) fmt.Print(a.String())
Output: CONNACK ---- -------s 5 bytes NotAuthorized!
func NewConnAck ¶
func NewConnAck() *ConnAck
func (*ConnAck) AssignedClientID ¶
func (*ConnAck) AuthMethod ¶
func (*ConnAck) MaxPacketSize ¶
func (*ConnAck) ReasonCode ¶
func (p *ConnAck) ReasonCode() ReasonCode
func (*ConnAck) ReasonString ¶
func (*ConnAck) ReceiveMax ¶
func (*ConnAck) ResponseInformation ¶
func (*ConnAck) RetainAvailable ¶
func (*ConnAck) ServerKeepAlive ¶
func (*ConnAck) ServerReference ¶
func (*ConnAck) SessionExpiryInterval ¶
func (*ConnAck) SessionPresent ¶
func (*ConnAck) SetAssignedClientID ¶
func (*ConnAck) SetAuthData ¶
func (*ConnAck) SetAuthMethod ¶
func (*ConnAck) SetMaxPacketSize ¶
func (*ConnAck) SetReasonCode ¶
func (p *ConnAck) SetReasonCode(v ReasonCode)
func (*ConnAck) SetReasonString ¶
func (*ConnAck) SetReceiveMax ¶
func (*ConnAck) SetResponseInformation ¶
func (*ConnAck) SetRetainAvailable ¶
func (*ConnAck) SetServerKeepAlive ¶
func (*ConnAck) SetServerReference ¶
func (*ConnAck) SetSessionExpiryInterval ¶
func (*ConnAck) SetSessionPresent ¶
func (*ConnAck) SetSharedSubAvailable ¶
func (*ConnAck) SetSubIdentifiersAvailable ¶
func (*ConnAck) SetTopicAliasMax ¶
func (*ConnAck) SetWildcardSubAvailable ¶
func (*ConnAck) SharedSubAvailable ¶
func (*ConnAck) String ¶
Example ¶
a := NewConnAck() a.SetSessionPresent(true) a.SetAssignedClientID("pink") a.SetReasonCode(QoSNotSupported) a.SetReasonString("please select max 1") fmt.Println(a.String()) fmt.Print(DocumentFlags(a))
Output: CONNACK ---- -------s pink 34 bytes QoSNotSupported! please select max 1 3210 76543210 AssignedClientID Size [ReasonCode and ReasonString if error] 3-0 reserved 7-1 reserved 0 s Session present
func (*ConnAck) SubIdentifiersAvailable ¶
func (*ConnAck) TopicAliasMax ¶
func (*ConnAck) UnmarshalBinary ¶
func (*ConnAck) WildcardSubAvailable ¶
type Connect ¶
type Connect struct { UserProperties // contains filtered or unexported fields }
Example ¶
c := NewConnect() c.SetClientID("macy") c.SetKeepAlive(299) c.SetUsername("john.doe") c.SetPassword([]byte("123")) fmt.Print(c.String())
Output: CONNECT ---- up------ MQTT5 macy 4m59s 34 bytes
func (*Connect) AuthMethod ¶
func (*Connect) CleanStart ¶ added in v0.24.0
func (*Connect) MaxPacketSize ¶
func (*Connect) ProtocolName ¶
func (*Connect) ProtocolVersion ¶
func (*Connect) ReceiveMax ¶
func (*Connect) RequestProblemInfo ¶
func (*Connect) RequestResponseInfo ¶
func (*Connect) SessionExpiryInterval ¶
func (*Connect) SetAuthData ¶
func (*Connect) SetAuthMethod ¶
func (*Connect) SetCleanStart ¶
func (*Connect) SetClientID ¶
func (*Connect) SetKeepAlive ¶
func (*Connect) SetMaxPacketSize ¶
func (*Connect) SetPassword ¶
func (*Connect) SetProtocolName ¶
func (*Connect) SetProtocolVersion ¶
func (*Connect) SetReceiveMax ¶
func (*Connect) SetRequestProblemInfo ¶
The Client uses this value to indicate whether the ReasonString String or User Properties are sent in the case of failures.
func (*Connect) SetRequestResponseInfo ¶
The Client uses this value to request the Server to return Response Information in the CONNACK
func (*Connect) SetSessionExpiryInterval ¶
func (*Connect) SetTopicAliasMax ¶
This value indicates the highest value that the Client will accept as a Topic Alias sent by the Server. The Client uses this value to limit the number of Topic Aliases that it is willing to hold on this Connection.
func (*Connect) SetUsername ¶
func (*Connect) SetWill ¶ added in v0.21.0
SetWill sets the will message and delay in seconds. The Server delays publishing the Client’s Will Message until the Will Delay Interval has passed or the Session ends, whichever happens first.
func (*Connect) String ¶
String returns a short string describing the connect packet.
Example ¶
p := NewConnect() p.SetClientID("pink") p.SetUsername("gopher") p.SetPassword([]byte("cute")) p.SetWill(Pub(1, "client/gone", "pink"), 3) fmt.Println(p.String()) fmt.Println(DocumentFlags(p))
Output: CONNECT ---- up--1w-- MQTT5 pink 0s 58 bytes 3210 76543210 ProtocolVersion ClientID KeepAlive Size 3-0 reserved 7 u User Name Flag 6 p Password Flag 5 r Will Retain 4 2|! Will QoS 3 1|! Will QoS 2 w Will Flag 1 s Clean Start 0 reserved
func (*Connect) TopicAliasMax ¶
func (*Connect) UnmarshalBinary ¶
type ControlPacket ¶
type ControlPacket interface { // Write the packet in wireformat to a writer io.WriterTo // Unmarshal wireformat encoding.BinaryUnmarshaler // Return a short readable string suitable for logging fmt.Stringer }
func ReadPacket ¶
func ReadPacket(r io.Reader) (ControlPacket, error)
ReadPacket reads one packet from the reader. Returns a io.EOF or Malformed error on failure.
type Disconnect ¶
type Disconnect struct { UserProperties // contains filtered or unexported fields }
func NewDisconnect ¶
func NewDisconnect() *Disconnect
NewDisconnect returns a disconnect packet with reason code NormalDisconnect.
func (*Disconnect) ReasonCode ¶
func (p *Disconnect) ReasonCode() ReasonCode
func (*Disconnect) SetReasonCode ¶
func (p *Disconnect) SetReasonCode(v ReasonCode)
func (*Disconnect) String ¶
func (p *Disconnect) String() string
Example ¶
p := NewDisconnect() fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: DISCONNECT ---- 2 bytes 3210 Size 3-0 reserved
func (*Disconnect) UnmarshalBinary ¶
func (p *Disconnect) UnmarshalBinary(data []byte) error
type HasPacketID ¶ added in v0.17.0
type HasPacketID interface {
PacketID() uint16
}
HasPacketID is implemented by packets carrying a packet ID.
type HasReason ¶ added in v0.25.0
type HasReason interface {
ReasonCode() ReasonCode
}
HasReason is implemented by packets carrying a reason code.
type HasWellFormed ¶ added in v0.26.0
type HasWellFormed interface {
WellFormed() *Malformed
}
HasWellFormed is implemented by packets that implement WellFormed.
type Ident ¶
type Ident uint8
MQTT Packet UserProp identifier codes Ident is the same as wuint16 but is used to name the identifier codes
const ( PayloadFormatIndicator Ident = 0x01 MessageExpiryInterval Ident = 0x02 ContentType Ident = 0x03 ResponseTopic Ident = 0x08 CorrelationData Ident = 0x09 SubscriptionID Ident = 0x0b SessionExpiryInterval Ident = 0x11 AssignedClientID Ident = 0x12 ServerKeepAlive Ident = 0x13 AuthMethod Ident = 0x15 AuthData Ident = 0x16 RequestProblemInfo Ident = 0x17 WillDelayInterval Ident = 0x18 RequestResponseInfo Ident = 0x19 ResponseInformation Ident = 0x1a ServerReference Ident = 0x1c ReasonString Ident = 0x1f ReceiveMax Ident = 0x21 TopicAliasMax Ident = 0x22 TopicAlias Ident = 0x23 MaxQoS Ident = 0x24 RetainAvailable Ident = 0x25 UserProperty Ident = 0x26 MaxPacketSize Ident = 0x27 WildcardSubAvailable Ident = 0x28 SubIDsAvailable Ident = 0x29 )
func (*Ident) UnmarshalBinary ¶
type Malformed ¶
type Malformed struct {
// contains filtered or unexported fields
}
func (*Malformed) Error ¶
Example ¶
var e Malformed e.SetPacket(NewConnect()) e.SetReasonString("missing data") fmt.Println(e.Error())
Output: malformed *mq.Connect: missing data
func (*Malformed) SetReasonString ¶ added in v0.25.0
type Opt ¶ added in v0.16.0
type Opt = FilterOption
type PingReq ¶
type PingReq struct {
// contains filtered or unexported fields
}
func NewPingReq ¶
func NewPingReq() *PingReq
func (*PingReq) String ¶
Example ¶
p := NewPingReq() fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: PINGREQ ---- 2 bytes 3210 Size 3-0 reserved
func (*PingReq) UnmarshalBinary ¶
type PingResp ¶
type PingResp struct {
// contains filtered or unexported fields
}
func NewPingResp ¶
func NewPingResp() *PingResp
func (*PingResp) String ¶
Example ¶
p := NewPingResp() fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: PINGRESP ---- 2 bytes 3210 Size 3-0 reserved
func (*PingResp) UnmarshalBinary ¶
type PubAck ¶
type PubAck struct { UserProperties // contains filtered or unexported fields }
A PubAck packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP
func (*PubAck) ReasonCode ¶
func (p *PubAck) ReasonCode() ReasonCode
func (*PubAck) ReasonString ¶ added in v0.25.0
func (*PubAck) SetPacketID ¶
func (*PubAck) SetReasonCode ¶
func (p *PubAck) SetReasonCode(v ReasonCode)
func (*PubAck) SetReasonString ¶ added in v0.25.0
func (*PubAck) String ¶
Example ¶
p := NewPubAck() p.SetPacketID(9) fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: PUBACK ---- p9 4 bytes 3210 PacketID ReasonString Size [reason text] 3-0 reserved
func (*PubAck) UnmarshalBinary ¶
type PubComp ¶ added in v0.23.0
type PubComp struct { UserProperties // contains filtered or unexported fields }
A PubComp packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP
func NewPubComp ¶
func NewPubComp() *PubComp
NewPubComp returns control packet with type PUBCOMP
Example ¶
p := NewPubComp() p.SetPacketID(9) fmt.Println(p)
Output: PUBCOMP ---- p9 Success 4 bytes
func (*PubComp) ReasonCode ¶ added in v0.23.0
func (p *PubComp) ReasonCode() ReasonCode
func (*PubComp) ReasonString ¶ added in v0.25.0
func (*PubComp) SetPacketID ¶ added in v0.23.0
func (*PubComp) SetReasonCode ¶ added in v0.23.0
func (p *PubComp) SetReasonCode(v ReasonCode)
func (*PubComp) SetReasonString ¶ added in v0.25.0
func (*PubComp) UnmarshalBinary ¶ added in v0.23.0
type PubRec ¶ added in v0.23.0
type PubRec struct { UserProperties // contains filtered or unexported fields }
A PubRec packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP
func NewPubRec ¶
func NewPubRec() *PubRec
NewPubRec returns control packet with type PUBREC
Example ¶
p := NewPubRec() p.SetPacketID(9) fmt.Println(p)
Output: PUBREC ---- p9 Success 4 bytes
func (*PubRec) ReasonCode ¶ added in v0.23.0
func (p *PubRec) ReasonCode() ReasonCode
func (*PubRec) ReasonString ¶ added in v0.25.0
func (*PubRec) SetPacketID ¶ added in v0.23.0
func (*PubRec) SetReasonCode ¶ added in v0.23.0
func (p *PubRec) SetReasonCode(v ReasonCode)
func (*PubRec) SetReasonString ¶ added in v0.25.0
func (*PubRec) UnmarshalBinary ¶ added in v0.23.0
type PubRel ¶ added in v0.21.0
type PubRel struct { UserProperties // contains filtered or unexported fields }
A PubRel packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP
func NewPubRel ¶
func NewPubRel() *PubRel
NewPubRel returns control packet with type PUBREL
Example ¶
p := NewPubRel() p.SetPacketID(9) p.SetReasonCode(PacketIdentifierNotFound) fmt.Println(p)
Output: PUBREL --1- p9 5 bytes PacketIdentifierNotFound!
func (*PubRel) ReasonCode ¶ added in v0.23.0
func (p *PubRel) ReasonCode() ReasonCode
func (*PubRel) ReasonString ¶ added in v0.25.0
func (*PubRel) SetPacketID ¶ added in v0.23.0
func (*PubRel) SetReasonCode ¶ added in v0.23.0
func (p *PubRel) SetReasonCode(v ReasonCode)
func (*PubRel) SetReasonString ¶ added in v0.25.0
func (*PubRel) UnmarshalBinary ¶ added in v0.23.0
type Publish ¶
type Publish struct { UserProperties // contains filtered or unexported fields }
Example (StringMalformed) ¶
fmt.Println(Pub(0, "", "gopher")) fmt.Println(Pub(3, "a/b", "gopher")) fmt.Println(Pub(1, "a/b", "gopher"))
Output: PUBLISH ---- p0 11 bytes, malformed! empty topic name PUBLISH -!!- p0 a/b 14 bytes, malformed! invalid QoS PUBLISH --1- p0 a/b 16 bytes, malformed! empty packet ID
func NewPublish ¶
func NewPublish() *Publish
func (*Publish) AddSubscriptionID ¶
func (*Publish) ContentType ¶
func (*Publish) CorrelationData ¶
func (*Publish) MessageExpiryInterval ¶
func (*Publish) PayloadFormat ¶
func (*Publish) ResponseTopic ¶
func (*Publish) SetContentType ¶
The value of the Content Type is defined by the sending and receiving application, e.g. it may be a mime type like application/json.
func (*Publish) SetCorrelationData ¶
func (*Publish) SetDuplicate ¶
func (*Publish) SetMessageExpiryInterval ¶
func (*Publish) SetPacketID ¶
func (*Publish) SetPayload ¶
func (*Publish) SetPayloadFormat ¶
SetPayloadFormat, false indicates that the message is unspecified bytes. True indicates that the message is UTF-8 encoded character data.
func (*Publish) SetQoS ¶
SetQoS, 0,1,2 or 3 other values unset the QoS. 3 is malformed but allowed to be set here.
func (*Publish) SetResponseTopic ¶
func (*Publish) SetTopicAlias ¶
func (*Publish) SetTopicName ¶
func (*Publish) String ¶
Example ¶
p := Pub(2, "a/b/1", "gopher") p.SetPacketID(3) p.SetRetain(true) p.SetCorrelationData([]byte("1111-222222-3333333")) fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: PUBLISH -2-r p3 a/b/1 1111-222222-3333333 40 bytes 3210 PacketID Topic [CorrelationData] Size 3 d Duplicate 2 2|! QoS 1 1|! QoS 0 r Retain
func (*Publish) SubscriptionIDs ¶
func (*Publish) TopicAlias ¶
func (*Publish) UnmarshalBinary ¶
func (*Publish) WellFormed ¶ added in v0.21.0
type ReasonCode ¶
type ReasonCode byte
const ( Success ReasonCode = 0x00 // ConnAck, PubAck, PubRec, PubRel, PubComp, UnsubAck, Auth NormalDisconnect ReasonCode = 0x00 // Disconnect GrantedQoS0 ReasonCode = 0x00 // SubAck GrantedQoS1 ReasonCode = 0x01 // SubAck GrantedQoS2 ReasonCode = 0x02 // SubAck DisconnectWithWill ReasonCode = 0x04 // Disconnect NoMatchingSubscribers ReasonCode = 0x10 // PubAck, PubRec NoSubscriptionExisted ReasonCode = 0x11 // UnsubAck ContinueAuth ReasonCode = 0x18 // Auth ReAuthenticate ReasonCode = 0x19 // Auth // failures >= 0x80 UnspecifiedError ReasonCode = 0x80 // ConnAck, PubAck, PubRec, SubAck, UnsubAck, Disconnect MalformedPacket ReasonCode = 0x81 // ConnAck, Disconnect ProtocolError ReasonCode = 0x82 // ConnAck, Disconnect ImplementationSpecificError ReasonCode = 0x83 // ConnAck, PubAck, PubRec, SubAck, UnsubAck, Disconnect UnsupportedProtocolVersion ReasonCode = 0x84 // ConnAck ClientIdentifierNotValid ReasonCode = 0x85 // ConnAck BadUserNameOrPassword ReasonCode = 0x86 // ConnAck NotAuthorized ReasonCode = 0x87 // ConnAck, PubAck, PubRec, SubAck, UnsubAck, Disconnect ServerBusy ReasonCode = 0x89 // ConnAck, Disconnect Banned ReasonCode = 0x8A // ConnAck ServerShuttingDown ReasonCode = 0x8B // Disconnect BadAuthenticationMethod ReasonCode = 0x8C // ConnAck, Disconnect KeepAliveTimeout ReasonCode = 0x8D // Disconnect SessionTakenOver ReasonCode = 0x8E // Disconnect TopicFilterInvalid ReasonCode = 0x8F // SubAck, UnsubAck, Disconnect TopicNameInvalid ReasonCode = 0x90 // ConnAck, PubAck, PubRec, Disconnect PacketIdentifierInUse ReasonCode = 0x91 // PubAck, PubRec, SubAck, UnsubAck PacketIdentifierNotFound ReasonCode = 0x92 // PubRel, PubComp ReceiveMaximumExceeded ReasonCode = 0x93 // Disconnect TopicAliasInvalid ReasonCode = 0x94 // Disconnect PacketTooLarge ReasonCode = 0x95 // ConnAck, Disconnect MessageRateToHigh ReasonCode = 0x96 // Disconnect QuotaExceeded ReasonCode = 0x97 // ConnAck, PubAck, PubRec, SubAck, Disconnect AdministrativeAction ReasonCode = 0x98 // Disconnect PayloadFormatInvalid ReasonCode = 0x99 // ConnAck, PubAck, PubRec, Disconnect RetainNotSupported ReasonCode = 0x9A // ConnAck, Disconnect QoSNotSupported ReasonCode = 0x9B // ConnAck, Disconnect UseAnotherServer ReasonCode = 0x9C // ConnAck, Disconnect ServerMoved ReasonCode = 0x9D // ConnAck, Disconnect ConnectionRateExceeded ReasonCode = 0x9F // ConnAck, Disconnect MaximumConnectTime ReasonCode = 0xA0 // Disconnect SubscriptionIdentifiersNotSupported ReasonCode = 0xA1 // SubAck, Disconnect WildcardSubscriptionsNotSupported ReasonCode = 0xA2 // SubAck, Disconnect )
func (ReasonCode) String ¶
func (i ReasonCode) String() string
type SubAck ¶
type SubAck struct { UserProperties // contains filtered or unexported fields }
func (*SubAck) AddReasonCode ¶
func (p *SubAck) AddReasonCode(v ReasonCode)
func (*SubAck) ReasonCodes ¶
func (*SubAck) ReasonString ¶
func (*SubAck) SetPacketID ¶
func (*SubAck) SetReasonString ¶
func (*SubAck) String ¶
Example ¶
p := NewSubAck() p.SetPacketID(3) fmt.Println(p) fmt.Print(DocumentFlags(p))
Output: SUBACK ---- p3 5 bytes 3210 PacketID Size 3-0 reserved
func (*SubAck) UnmarshalBinary ¶
type Subscribe ¶
type Subscribe struct { UserProperties // contains filtered or unexported fields }
Example (Malformed) ¶
// no filters fmt.Println(NewSubscribe()) { // bad qos s := NewSubscribe() s.SetSubscriptionID(1) s.AddFilters(NewTopicFilter("a/b", OptQoS3)) fmt.Println(s) } { // empty filter s := NewSubscribe() s.SetSubscriptionID(1) s.AddFilters(NewTopicFilter("", OptQoS1)) fmt.Println(s) } { // missing subscription id s := NewSubscribe() s.SetSubscriptionID(0) s.AddFilters(NewTopicFilter("#", OptQoS1)) fmt.Println(s) } { // too large subscription id s := NewSubscribe() s.SetSubscriptionID(268_435_455 + 1) s.AddFilters(NewTopicFilter("#", OptQoS1)) fmt.Println(s) }
Output: SUBSCRIBE --1- p0 5 bytes, malformed! no filters SUBSCRIBE --1- p0 a/b --r0--!! 13 bytes, malformed! invalid QoS SUBSCRIBE --1- p0 --r0---1 10 bytes, malformed! empty filter SUBSCRIBE --1- p0 # --r0---1 9 bytes, malformed! missing sub ID SUBSCRIBE --1- p0 # --r0---1 15 bytes, malformed! too large sub ID
func NewSubscribe ¶
func NewSubscribe() *Subscribe
func (*Subscribe) AddFilters ¶ added in v0.26.0
func (p *Subscribe) AddFilters(v ...TopicFilter)
func (*Subscribe) Filters ¶ added in v0.24.0
func (p *Subscribe) Filters() []TopicFilter
func (*Subscribe) SetPacketID ¶
func (*Subscribe) SetSubscriptionID ¶
func (*Subscribe) String ¶
Example ¶
s := NewSubscribe() s.SetSubscriptionID(39) s.AddUserProp("color", "purple") s.AddFilters( NewTopicFilter("a/b/c", OptQoS2|OptNL|OptRAP), NewTopicFilter("d/e", OptQoS1), ) fmt.Println(s)
Output: SUBSCRIBE --1- p0 a/b/c --r0pn2- 37 bytes
func (*Subscribe) SubscriptionID ¶
func (*Subscribe) UnmarshalBinary ¶
func (*Subscribe) WellFormed ¶ added in v0.26.0
type TopicFilter ¶
type TopicFilter struct {
// contains filtered or unexported fields
}
func NewTopicFilter ¶
func NewTopicFilter(filter string, options Opt) TopicFilter
func (*TopicFilter) Filter ¶ added in v0.26.0
func (c *TopicFilter) Filter() string
func (*TopicFilter) Options ¶ added in v0.26.0
func (c *TopicFilter) Options() Opt
func (*TopicFilter) SetFilter ¶ added in v0.26.0
func (c *TopicFilter) SetFilter(v string)
func (*TopicFilter) SetOptions ¶ added in v0.26.0
func (c *TopicFilter) SetOptions(v Opt)
func (TopicFilter) String ¶
func (c TopicFilter) String() string
Example ¶
fmt.Println(NewTopicFilter("gopher/pink/#", OptQoS1|OptRetain1))
Output: gopher/pink/# --r1---1
func (*TopicFilter) WellFormed ¶ added in v0.26.0
func (c *TopicFilter) WellFormed() *Malformed
type Undefined ¶
type Undefined struct {
// contains filtered or unexported fields
}
Undefined represents a packet with type value of 0.
func (*Undefined) UnmarshalBinary ¶
type UnsubAck ¶
type UnsubAck struct { UserProperties // contains filtered or unexported fields }
func NewUnsubAck ¶
func NewUnsubAck() *UnsubAck
func (*UnsubAck) AddReasonCode ¶
func (p *UnsubAck) AddReasonCode(v ReasonCode)
func (*UnsubAck) ReasonCodes ¶
func (*UnsubAck) ReasonString ¶
func (*UnsubAck) SetPacketID ¶
func (*UnsubAck) SetReasonString ¶
func (*UnsubAck) UnmarshalBinary ¶
type Unsubscribe ¶
type Unsubscribe struct { UserProperties // contains filtered or unexported fields }
func NewUnsubscribe ¶
func NewUnsubscribe() *Unsubscribe
func (*Unsubscribe) AddFilter ¶
func (p *Unsubscribe) AddFilter(filter string)
func (*Unsubscribe) PacketID ¶
func (p *Unsubscribe) PacketID() uint16
func (*Unsubscribe) SetPacketID ¶
func (p *Unsubscribe) SetPacketID(v uint16)
func (*Unsubscribe) String ¶
func (p *Unsubscribe) String() string
func (*Unsubscribe) UnmarshalBinary ¶
func (p *Unsubscribe) UnmarshalBinary(data []byte) error
type UserProp ¶ added in v0.21.0
type UserProp [2]string
https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901013
func (*UserProp) UnmarshalBinary ¶ added in v0.21.0
type UserProperties ¶ added in v0.21.0
type UserProperties []UserProp
func (*UserProperties) AddUserProp ¶ added in v0.21.0
func (p *UserProperties) AddUserProp(kvPair ...string)
AddUserProp adds key value pair user properties. The same key is is allowed to appear more than once.
Example ¶
var u UserProperties u.AddUserProp( "size", "large", "color", "red", ) fmt.Println(u)
Output: [size:large color:red]