Documentation ¶
Index ¶
- Constants
- Variables
- func ListenAndServe(addr string, server Server) error
- func ListenAndServeTLS(addr string, config *tls.Config, server Server) error
- func Serve(listener net.Listener, server Server) error
- type Authenticate
- type Client
- func (client *Client) Acknowledge(mid int)
- func (client *Client) Disconnect() error
- func (client *Client) ID() string
- func (client *Client) Message() (*Message, error)
- func (client *Client) NumPending() int
- func (client *Client) Packet() (Packet, *Message, error)
- func (client *Client) Ping() error
- func (client *Client) Publish(msg *Message) error
- func (client *Client) Send(pkt Packet) error
- func (client *Client) SetLogger(logger *log.Logger, level LogLevel)
- func (client *Client) Subscribe(topic string, qos byte) (byte, error)
- func (client *Client) SubscribeAll(topics []TopicSubscription) ([]byte, error)
- func (client *Client) Unsubscribe(topics ...string)
- func (client *Client) Will() *Message
- type ConnAckPacket
- type ConnectAuth
- type ConnectCode
- type ConnectPacket
- type DisconnectPacket
- type FixedHeader
- type LogLevel
- type Message
- type Packet
- type PacketType
- type PingReqPacket
- type PingRespPacket
- type Property
- type PubAckPacket
- type PubCompPacket
- type PubRecPacket
- type PubRelPacket
- type PublishPacket
- type QoS
- type ReasonCode
- type Reciever
- type RecieverFunc
- type Sender
- type Server
- type Stream
- type SubAckPacket
- type SubscribePacket
- type Subscription
- type TopicSubscription
- type UnsubAckPacket
- type UnsubscribePacket
Constants ¶
const ( CONNECT PacketType = 1 CONNACK = 2 PUBLISH = 3 PUBACK = 4 PUBREC = 5 PUBREL = 6 PUBCOMP = 7 SUBSCRIBE = 8 SUBACK = 9 UNSUBSCRIBE = 10 UNSUBACK = 11 PINGREQ = 12 PINGRESP = 13 DISCONNECT = 14 )
List of packet types
const ( PropPayloadFormat Property = 0x01 // Payload Format Indicator PropMsgExpiry = 0x02 // Message Expiry Interval PropContentType = 0x03 // Content Type PropRespTopic = 0x08 // Response Topic PropCorrelData = 0x09 // Correlation Data PropSubsIdent = 0x0B // Subscription Identifier PropSessExpiry = 0x11 // Session Expiry Interval PropClientIdet = 0x12 // Assigned Client Identifier PropServerKeepAlive = 0x13 // Server Keep Alive PropAuthMethod = 0x15 // Authentication Method PropAuthData = 0x16 // Authentication Data PropReqProblem = 0x17 // Request Problem Information PropWillDelay = 0x18 // Will Delay Interval PropReqRespInfo = 0x19 // Request Response Information PropRespInfo = 0x1A // Response Information PropServerRef = 0x1C // Server Reference PropReason = 0x1F // Reason String PropRecvMax = 0x21 // Receive Maximum PropTopicAliasMax = 0x22 // Topic Alias Maximum PropTopicAlias = 0x23 // Topic Alias PropMaxQoS = 0x24 // Maximum QoS PropRetainAvailable = 0x25 // Retain Available PropUser = 0x26 // User Property PropMaxPacket = 0x27 // Maximum Packet Size PropWildcardSubsAvail = 0x28 // Wildcard Subscription Available PropSubsIdentAvail = 0x29 // Subscription Identifier Available )
Packet Properties
Variables ¶
var ErrMaxPending = errors.New("reached max pending")
var ErrServerClose = errors.New("server close")
var ErrSessionOvertake = errors.New("session overtaken")
var InvalidQoS = errors.New("invalid QoS")
var MaxPending = 16
Functions ¶
func ListenAndServe ¶
ListenAndServe listens at the give tcp address.
func ListenAndServeTLS ¶
ListenAndServeTLS listens at the give tcp address.
Types ¶
type Authenticate ¶
type Authenticate func(client *Client, auth *ConnectAuth) ConnectCode
Authenticate is a simple function to check a client and its authentication.
type Client ¶
type Client struct { MaxPending int LogLevel LogLevel Server Server // contains filtered or unexported fields }
Client is a MQTT Client. Use `Dial` to create a new client.
func Dial ¶
func Dial(addr string, id string, cleanSession bool, auth *ConnectAuth, will *Message) (*Client, error)
Dial connects to a remote MQTT server.
func (*Client) Acknowledge ¶
func (*Client) Disconnect ¶
func (*Client) NumPending ¶
NumPending gives the number of outstanding QoS>0 packets that have now been acknowledged yet.
func (*Client) SubscribeAll ¶
func (client *Client) SubscribeAll(topics []TopicSubscription) ([]byte, error)
func (*Client) Unsubscribe ¶
type ConnAckPacket ¶
type ConnAckPacket struct { SessionPresent bool Code ConnectCode // contains filtered or unexported fields }
ConnAckPacket is a MQTT CONNACK control packet.
func ConnAck ¶
func ConnAck(code ConnectCode, sessPresent bool) *ConnAckPacket
ConnAck creates a new MQTT CONNACK control packet.
func (*ConnAckPacket) Header ¶
func (pkt *ConnAckPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*ConnAckPacket) String ¶
func (pkt *ConnAckPacket) String() string
String stringifies the packet in a human readable format.
type ConnectAuth ¶
type ConnectAuth struct {
Username, Password string
}
ConnectAuth is the authentication used with Connect packets.
type ConnectCode ¶
type ConnectCode byte
const ( CodeAccepted ConnectCode = iota CodeUnacceptableProtoV CodeIDentifierRejected CodeBatUserOrPassword CodeNotAuthorized )
func (ConnectCode) String ¶
func (c ConnectCode) String() string
type ConnectPacket ¶
type ConnectPacket struct { // Variable Header Protocol string Version byte CleanSession bool KeepAliveTimer int // Payload ClientID string Will *Message Auth *ConnectAuth // contains filtered or unexported fields }
ConnectPacket is a MQTT CONNECT control packet.
func Connect ¶
func Connect(protocol string, version byte, cleanSession bool, keepAliveTimer int, clientID string, will *Message, auth *ConnectAuth) *ConnectPacket
Connect creates a new MQTT CONNECT control packet.
func (*ConnectPacket) Header ¶
func (pkt *ConnectPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*ConnectPacket) String ¶
func (pkt *ConnectPacket) String() string
String stringifies the packet in a human readable format.
type DisconnectPacket ¶
type DisconnectPacket struct {
// contains filtered or unexported fields
}
DisconnectPacket is a MQTT DISCONNECT control packet.
func Disconnect ¶
func Disconnect() *DisconnectPacket
Disconnect creates a new MQTT DISCONNECT packet.
func (*DisconnectPacket) Header ¶
func (pkt *DisconnectPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*DisconnectPacket) String ¶
func (pkt *DisconnectPacket) String() string
String stringifies the packet in a human readable format.
type FixedHeader ¶
type FixedHeader struct { PacketType PacketType Dup bool QoS byte Retain bool Length int }
FixedHeader is used with every package.
func (*FixedHeader) Read ¶
func (fh *FixedHeader) Read(reader io.Reader) (int, error)
Read reads a FixedHeader from an io.Reader.
func (*FixedHeader) ReadBuffer ¶
func (fh *FixedHeader) ReadBuffer(buf []byte) (int, error)
Read reads a FixedHeader from an io.Reader.
type Message ¶
type Message struct { // Topic of this message. Topic string // Data Data []byte // QoS = Quality of Service QoS byte // Retain is true if the message is a Retain Message. Retain bool }
Message is a published message for a topic a the given QoS.
type Packet ¶
type Packet interface { WriteTo(w io.Writer, version byte) (len int, err error) Header() *FixedHeader String() string }
type PacketType ¶
type PacketType byte
PacketType is the MQTT Control Packet type. See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901022
func (PacketType) String ¶
func (p PacketType) String() string
type PingReqPacket ¶
type PingReqPacket struct {
// contains filtered or unexported fields
}
PingReqPacket is a MQTT PINGREQ control packet.
func (*PingReqPacket) Header ¶
func (pkt *PingReqPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PingReqPacket) String ¶
func (pkt *PingReqPacket) String() string
String stringifies the packet in a human readable format.
type PingRespPacket ¶
type PingRespPacket struct {
// contains filtered or unexported fields
}
PingRespPacket is a MQTT PINGRESP control packet.
func (*PingRespPacket) Header ¶
func (pkt *PingRespPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PingRespPacket) String ¶
func (pkt *PingRespPacket) String() string
String stringifies the packet in a human readable format.
type Property ¶
type Property byte
Property fields See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901027
type PubAckPacket ¶
type PubAckPacket struct { ID int // contains filtered or unexported fields }
PubAckPacket is a MQTT PUBACK control packet.
func (*PubAckPacket) Header ¶
func (pkt *PubAckPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PubAckPacket) String ¶
func (pkt *PubAckPacket) String() string
String stringifies the packet in a human readable format.
type PubCompPacket ¶
type PubCompPacket struct { ID int // contains filtered or unexported fields }
PubCompPacket is a MQTT PUBCOMP control packet.
func (*PubCompPacket) Header ¶
func (pkt *PubCompPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PubCompPacket) String ¶
func (pkt *PubCompPacket) String() string
String stringifies the packet in a human readable format.
type PubRecPacket ¶
type PubRecPacket struct { ID int // contains filtered or unexported fields }
PubRecPacket is a MQTT PUBREC control packet.
func (*PubRecPacket) Header ¶
func (pkt *PubRecPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PubRecPacket) String ¶
func (pkt *PubRecPacket) String() string
String stringifies the packet in a human readable format.
type PubRelPacket ¶
type PubRelPacket struct { ID int // contains filtered or unexported fields }
PubRelPacket is a MQTT PUBREL control packet.
func (*PubRelPacket) Header ¶
func (pkt *PubRelPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PubRelPacket) String ¶
func (pkt *PubRelPacket) String() string
String stringifies the packet in a human readable format.
type PublishPacket ¶
type PublishPacket struct { // Variable Header Topic string // Publish Topic ID int // Message ID // Payload Data []byte // contains filtered or unexported fields }
PublishPacket is a MQTT PUBLISH packet.
func Publish ¶
func Publish(id int, msg *Message) *PublishPacket
Publish creates a new MQTT PUBLISH control packet.
func (*PublishPacket) Header ¶
func (pkt *PublishPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*PublishPacket) Message ¶
func (pkt *PublishPacket) Message() *Message
Message returns the Message that this Publish packets transports.
func (*PublishPacket) String ¶
func (pkt *PublishPacket) String() string
String stringifies the packet in a human readable format.
type ReasonCode ¶
type ReasonCode byte
A ReasonCode is a one byte unsigned value that indicates the result of an operation. Reason Codes less than 0x80 indicate successful completion of an operation. The normal Reason Code for success is 0. Reason Code values of 0x80 or greater indicate failure. https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901031
const ( ReasonSuccess ReasonCode = 0x00 // Success ReasonDisconnect ReasonCode = 0x00 // Normal disconnection ReasonQoS0 ReasonCode = 0x00 // Granted QoS 0 ReasonQoS1 ReasonCode = 0x01 // Granted QoS 1 ReasonQoS2 ReasonCode = 0x02 // Granted QoS 2 ReasonDisconnectWill ReasonCode = 0x04 // Disconnect with Will Message ReasonNoSubsMatch ReasonCode = 0x10 // No matching subscribers ReasonNoSubsExist ReasonCode = 0x11 // No subscription existed ReasonContinueAuth ReasonCode = 0x18 // Continue authentication ReasonReAuth ReasonCode = 0x19 // Re-authenticate ReasonUnspecErr ReasonCode = 0x80 // Unspecified error ReasonMalfPacket ReasonCode = 0x81 // Malformed Packet ReasonProtoErr ReasonCode = 0x82 // Protocol Error ReasonImplSpec ReasonCode = 0x83 // Implementation specific ReasonUnsupProtoV ReasonCode = 0x84 // Unsupported Protocol Version ReasonClientIDInvalid ReasonCode = 0x85 // Client Identifier not valid ReasonBadAuth ReasonCode = 0x86 // Bad User Name or Password ReasonNotAuth ReasonCode = 0x87 // Not authorized ReasonBusy ReasonCode = 0x89 // Server busy ReasonBanned ReasonCode = 0x8A // Banned ReasonSuttingDown ReasonCode = 0x8B // Server shutting down ReasonBadAuthMehtod ReasonCode = 0x8C // Bad authentication method ReasonKeepAliveTimeout ReasonCode = 0x8D // Keep Alive timeout ReasonSessionTaken ReasonCode = 0x8E // Session taken over ReasonInvalFilter ReasonCode = 0x8F // Topic Filter invalid ReasonInvalName ReasonCode = 0x90 // Topic Name invalid ReasonIdentInUse ReasonCode = 0x91 // Packet Identifier in use ReasonIdentNotFound ReasonCode = 0x92 // Packet Identifier not found ReasonMaxReceive ReasonCode = 0x93 // Receive Maximum exceeded ReasonInvalAlias ReasonCode = 0x94 // Topic Alias invalid ReasonTooLarge ReasonCode = 0x95 // Packet too large ReasonHighRate ReasonCode = 0x96 // Message rate too high ReasonQuotaExceeded ReasonCode = 0x97 // Quota exceeded ReasonAdmin ReasonCode = 0x98 // Administrative action ReasonInvalFormat ReasonCode = 0x99 // Payload format invalid ReasonUnsupRetain ReasonCode = 0x9A // Retain not supported ReasonUnsupQoS ReasonCode = 0x9B // QoS not supported ReasonUseAnother ReasonCode = 0x9C // Use another server ReasonMoved ReasonCode = 0x9D // Server moved ReasonConnReateExceeded ReasonCode = 0x9F // Connection rate exceeded ReasonMaxConnTime ReasonCode = 0xA0 // Maximum connect time ReasonUnsupSubsIdent ReasonCode = 0xA1 // Subscription Identifiers not supported ReasonUnsupWildcard ReasonCode = 0xA2 // Wildcard Subscriptions not supported )
Reason Codes
func (ReasonCode) Error ¶
func (code ReasonCode) Error() error
Error transforms the RasonCode to an error. The error is nil if the reason is `ReasonSuccess` (which is not an error).
func (ReasonCode) String ¶
func (code ReasonCode) String() string
type RecieverFunc ¶
The RecieverFunc type is an adapter to allow the use of ordinary functions as Recievres.
func (RecieverFunc) ID ¶
func (f RecieverFunc) ID() string
type Server ¶
type Server interface { // Connect checks the ConnectAuth of a new client. Connect(client *Client, auth *ConnectAuth) ConnectCode // Close terminates the server an all client connection. Close() error // Serve makes the stream part of this server. This call is blocking and will read and write from the stream until the client or the server closes the connection. Serve(stream Stream) // Publish can be called from clients to emit new messages. Publish(sender Sender, msg *Message) int // Subscribe adds a new subscription to the topics tree. Subscribe(recv Reciever, topic string, qos byte) *Subscription // SubscribeAll adds a list of subscriptions to the topics tree. SubscribeAll(recv Reciever, topics []TopicSubscription) []*Subscription // Unsubscribe releases a subscription. Unsubscribe(subs ...*Subscription) // Disconnect removes a client that has diconnected. Disconnect(client *Client, reason error) }
Server represents an MQTT server.
type SubAckPacket ¶
type SubAckPacket struct { // Variable Header ID int // Message ID // Payload Granted []byte // List of granted QoS // contains filtered or unexported fields }
SubAckPacket is a MQTT SUBACK control packet.
func SubAck ¶
func SubAck(id int, granted []byte) *SubAckPacket
func (*SubAckPacket) Header ¶
func (pkt *SubAckPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*SubAckPacket) String ¶
func (pkt *SubAckPacket) String() string
String stringifies the packet in a human readable format.
type SubscribePacket ¶
type SubscribePacket struct { // Variable Header ID int // Message ID // Payload Topics []TopicSubscription // List of Subscriptions // contains filtered or unexported fields }
SubscribePacket is a MQTT SUBSCRIBE control packet.
func Subscribe ¶
func Subscribe(id int, topics []TopicSubscription) *SubscribePacket
Subscribe creates a new MQTT SUBSCRIBE control packet.
func (*SubscribePacket) Header ¶
func (pkt *SubscribePacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*SubscribePacket) String ¶
func (pkt *SubscribePacket) String() string
String stringifies the packet in a human readable format.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
func (*Subscription) QoS ¶
func (s *Subscription) QoS() byte
func (*Subscription) Topic ¶
func (s *Subscription) Topic() string
type TopicSubscription ¶
A TopicSubscription is used in Subscribe packets.
type UnsubAckPacket ¶
type UnsubAckPacket struct { // Variable Header ID int // Message ID // contains filtered or unexported fields }
UnsubAckPacket is a MQTT UNSUBACK control packet.
func UnsubAck ¶
func UnsubAck(id int) *UnsubAckPacket
UnsubAck creates a new MQTT UNSUCKACK control packet
func (*UnsubAckPacket) Header ¶
func (pkt *UnsubAckPacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*UnsubAckPacket) String ¶
func (pkt *UnsubAckPacket) String() string
String stringifies the packet in a human readable format.
type UnsubscribePacket ¶
type UnsubscribePacket struct { // Variable Header ID int // Message ID // Payload Topics []string // List of Topics to unsubscribe // contains filtered or unexported fields }
UnsubscribePacket is a MQTT UNSUBSCRIBE control packet.
func Unsubscribe ¶
func Unsubscribe(id int, topics []string) *UnsubscribePacket
Unsubscribe creates a new MQTT UNSUBSCRIBE control packet.
func (*UnsubscribePacket) Header ¶
func (pkt *UnsubscribePacket) Header() *FixedHeader
Header returns the FixedHeader of this packet.
func (*UnsubscribePacket) String ¶
func (pkt *UnsubscribePacket) String() string
String stringifies the packet in a human readable format.