Documentation ¶
Index ¶
- Variables
- type BufferWriter
- type Client
- type ConnAckCode
- type ConnAckPacket
- type ConnHandler
- type ConnPacket
- type CtrlType
- type FilePersist
- type LogLevel
- type MemPersist
- type NetHandler
- type Option
- func WithBackoffStrategy(firstDelay, maxDelay time.Duration, factor float64) Option
- func WithCleanSession(f bool) Option
- func WithClientID(clientID string) Option
- func WithDialTimeout(timeout uint16) Option
- func WithIdentity(username, password string) Option
- func WithKeepalive(keepalive uint16, factor float64) Option
- func WithLog(l LogLevel) Option
- func WithPersist(method PersistMethod) Option
- func WithRecvBuf(size int) Option
- func WithRouter(r TopicRouter) Option
- func WithSendBuf(size int) Option
- func WithServer(servers ...string) Option
- func WithTLS(certFile, keyFile string, caCert string, serverNameOverride string, ...) Option
- func WithWill(topic string, qos QosLevel, retain bool, payload []byte) Option
- type Packet
- type PersistHandler
- type PersistMethod
- type PersistStrategy
- type ProtocolLevel
- type PubAckPacket
- type PubCompPacket
- type PubHandler
- type PubRecvPacket
- type PubRelPacket
- type PublishPacket
- type QosLevel
- type RegexRouter
- type StandardRouter
- type SubAckCode
- type SubAckPacket
- type SubHandler
- type SubscribePacket
- type TextRouter
- type Topic
- type TopicHandler
- type TopicRouter
- type UnSubAckPacket
- type UnSubHandler
- type UnSubPacket
Constants ¶
This section is empty.
Variables ¶
var ( // PingReqPacket is the final instance of pingReqPacket PingReqPacket = &pingReqPacket{} // PingRespPacket is the final instance of pingRespPacket PingRespPacket = &pingRespPacket{} )
var (
// DisConnPacket is the final instance of disConnPacket
DisConnPacket = &disConnPacket{}
)
var ( // ErrBadPacket is the error happened when trying to decode a none MQTT packet ErrBadPacket = errors.New("decoded none MQTT packet ") )
var ( // ErrTimeOut connection timeout error ErrTimeOut = errors.New("connection timeout ") )
var NonePersist = &nonePersist{}
NonePersist defines no persist storage
var ( // PacketDroppedByStrategy used when persist store packet while strategy // don't allow that persist PacketDroppedByStrategy = errors.New("packet persist dropped by strategy ") )
Functions ¶
This section is empty.
Types ¶
type BufferWriter ¶
type BufferWriter interface { io.Writer io.ByteWriter }
type Client ¶
type Client interface { // Handle register topic handlers, mostly used for RegexHandler, RestHandler // the default handler inside the client is TextHandler, which match the exactly same topic Handle(topic string, h TopicHandler) // Connect to all specified server with client options Connect(ConnHandler) // Publish a message for the topic Publish(packets ...*PublishPacket) // Subscribe topic(s) Subscribe(topics ...*Topic) // UnSubscribe topic(s) UnSubscribe(topics ...string) // Wait will wait until all connection finished Wait() // Destroy all client connection Destroy(force bool) // handlers HandlePub(PubHandler) HandleSub(SubHandler) HandleUnSub(UnSubHandler) HandleNet(NetHandler) HandlePersist(PersistHandler) }
Client act as a mqtt client
type ConnAckCode ¶
type ConnAckCode = byte
ConnAckCode is connection response code from server
const ( // ConnAccepted client accepted by server ConnAccepted ConnAckCode = iota // ConnBadProtocol Protocol not supported ConnBadProtocol // ConnIDRejected Connection Id not valid ConnIDRejected ConnServerUnavailable // ConnBadIdentity Identity failed ConnBadIdentity // ConnAuthFail Auth failed ConnAuthFail )
type ConnAckPacket ¶
type ConnAckPacket struct { Present bool Code ConnAckCode }
ConnAckPacket is the packet sent by the Server in response to a ConnPacket received from a Client.
The first packet sent from the Server to the Client MUST be a ConnAckPacket
func (*ConnAckPacket) Type ¶
func (c *ConnAckPacket) Type() CtrlType
Type ConnAckPacket'strategy type is CtrlConnAck
func (*ConnAckPacket) WriteTo ¶
func (c *ConnAckPacket) WriteTo(w BufferWriter) error
WriteTo encode ConnAckPacket to bytes
type ConnHandler ¶
type ConnHandler func(server string, code ConnAckCode, err error)
ConnHandler is the handler which tend to the Connect result server is the server address provided by user in client creation call code is the ConnResult code err is the error happened when connect to server, if a error happened, the code value will max byte value (255)
type ConnPacket ¶
type ConnPacket struct { Username string Password string ClientID string CleanSession bool IsWill bool WillQos QosLevel WillRetain bool Keepalive uint16 WillTopic string WillMessage []byte // contains filtered or unexported fields }
ConnPacket is the first packet sent by Client to Server
func (*ConnPacket) Type ¶
func (c *ConnPacket) Type() CtrlType
Type ConnPacket'strategy type is CtrlConn
func (*ConnPacket) WriteTo ¶
func (c *ConnPacket) WriteTo(w BufferWriter) error
WriteTo encode ConnPacket to bytes
type CtrlType ¶
type CtrlType = byte
CtrlType is MQTT Control packet type
const ( // CtrlConn Connect CtrlConn CtrlType = iota + 1 // CtrlConnAck Connect Ack CtrlConnAck // CtrlPublish Publish CtrlPublish // CtrlPubAck Publish Ack CtrlPubAck // CtrlPubRecv Publish Received CtrlPubRecv // CtrlPubRel Publish Release CtrlPubRel // CtrlPubComp Publish Complete CtrlPubComp // CtrlSubscribe Subscribe CtrlSubscribe // CtrlSubAck Subscribe Ack CtrlSubAck // CtrlUnSub UnSubscribe CtrlUnSub // CtrlUnSubAck UnSubscribe Ack CtrlUnSubAck // CtrlPingReq Ping Request CtrlPingReq // CtrlPingResp Ping Response CtrlPingResp // CtrlDisConn Disconnect CtrlDisConn )
type FilePersist ¶
type FilePersist struct {
// contains filtered or unexported fields
}
FilePersist is the file persist method
func NewFilePersist ¶
func NewFilePersist(dirPath string, strategy *PersistStrategy) *FilePersist
NewFilePersist will create a file persist method with provided dirPath and strategy, if no strategy provided (nil), then the default strategy will be used
func (*FilePersist) Delete ¶
func (m *FilePersist) Delete(key string) error
Delete a persisted packet with key
func (*FilePersist) Load ¶
func (m *FilePersist) Load(key string) (Packet, bool)
Load a packet with key, return nil, false when no packet found
type MemPersist ¶
type MemPersist struct {
// contains filtered or unexported fields
}
MemPersist is the in memory persist method
func NewMemPersist ¶
func NewMemPersist(strategy *PersistStrategy) *MemPersist
NewMemPersist create a in memory persist method with provided strategy if no strategy provided (nil), then the default strategy will be used
func (*MemPersist) Delete ¶
func (m *MemPersist) Delete(key string) error
Delete a persisted packet with key
func (*MemPersist) Load ¶
func (m *MemPersist) Load(key string) (Packet, bool)
Load a packet with key, return nil, false when no packet found
type NetHandler ¶
NetHandler handles the error occurred when net broken
type Option ¶
type Option func(*client) error
Option is client option for connection options
func WithBackoffStrategy ¶
WithBackoffStrategy will set reconnect backoff strategy firstDelay is the time to wait before retrying after the first failure maxDelay defines the upper bound of backoff delay factor is applied to the backoff after each retry. e.g. FirstDelay = 1s and Factor = 2, then the SecondDelay is 2s, the ThirdDelay is 4s
func WithCleanSession ¶
WithCleanSession will set clean flag in connect packet
func WithClientID ¶
WithClientID set the client id for connection
func WithDialTimeout ¶
WithDialTimeout for connection time out (time in second)
func WithIdentity ¶
WithIdentity for username and password
func WithKeepalive ¶
WithKeepalive set the keepalive interval (time in second)
func WithPersist ¶
func WithPersist(method PersistMethod) Option
WithPersist defines the persist method to be used
func WithRecvBuf ¶
WithRecvBuf designate the channel size of receive
func WithRouter ¶
func WithRouter(r TopicRouter) Option
WithRouter set the router for topic dispatch
func WithServer ¶
WithServer adds servers as client server Just use "ip:port" or "domain.name:port" However, only TCP connection supported for now
type Packet ¶
type Packet interface { // Type return the packet type Type() CtrlType // WriteTo WriteTo(BufferWriter) error }
Packet is MQTT control packet
type PersistHandler ¶
type PersistHandler func(err error)
PersistHandler handles err happened when persist process has trouble
type PersistMethod ¶
type PersistMethod interface { // Name of what persist strategy used Name() string // Store a packet with key Store(key string, p Packet) error // Load a packet from stored data according to the key Load(key string) (Packet, bool) // Range over data stored, return false to break the range Range(func(key string, p Packet) bool) // Delete Delete(key string) error // Destroy stored data Destroy() error }
PersistMethod defines the behavior of persist methods
type PersistStrategy ¶
type PersistStrategy struct { // Interval applied to file/database persist // if this field is set to 0, means do persist per action // default value is 1s Interval time.Duration // MaxCount applied to all persist method // if this field set to 0, means no persist limit // for memory persist, means max in memory count // for file/database persist, means max entry in file/memory // default value is 0 MaxCount uint32 // DropOnExceed defines how to tackle with packets incoming // when max count is reached, default value is false DropOnExceed bool // DuplicateReplace defines whether duplicated key should // override previous one, default value is true DuplicateReplace bool }
PersistStrategy defines the details to be complied in persist methods
func DefaultPersistStrategy ¶
func DefaultPersistStrategy() *PersistStrategy
DefaultPersistStrategy will create a default PersistStrategy Interval = 1s, MaxCount = 0, DropOnExceed = false, DuplicateReplace = true
type ProtocolLevel ¶
type ProtocolLevel = byte
ProtocolLevel MQTT Protocol
const ( // V311 means MQTT 3.1.1 V311 ProtocolLevel = 4 // V5 means MQTT 5 V5 ProtocolLevel = 5 )
type PubAckPacket ¶
type PubAckPacket struct {
PacketID uint16
}
PubAckPacket is the response to a PublishPacket with QoS level 1.
func (*PubAckPacket) Type ¶
func (p *PubAckPacket) Type() CtrlType
Type PubAckPacket's type is CtrlPubAck
func (*PubAckPacket) WriteTo ¶
func (p *PubAckPacket) WriteTo(w BufferWriter) error
WriteTo encode PubAckPacket into buffer
type PubCompPacket ¶
type PubCompPacket struct {
PacketID uint16
}
PubCompPacket is the response to a PubRelPacket. It is the fourth and final packet of the QoS 892 2 protocol exchange. 893
func (*PubCompPacket) Type ¶
func (p *PubCompPacket) Type() CtrlType
Type PubCompPacket's type is CtrlPubComp
func (*PubCompPacket) WriteTo ¶
func (p *PubCompPacket) WriteTo(w BufferWriter) error
WriteTo encode PubCompPacket into buffer
type PubHandler ¶
PubHandler handles the error occurred when publish some message if err is not nil, that means a error occurred when sending pub msg
type PubRecvPacket ¶
type PubRecvPacket struct {
PacketID uint16
}
PubRecvPacket is the response to a PublishPacket with QoS 2. It is the second packet of the QoS 2 protocol exchange.
func (*PubRecvPacket) Type ¶
func (p *PubRecvPacket) Type() CtrlType
Type PubRecvPacket's type is CtrlPubRecv
func (*PubRecvPacket) WriteTo ¶
func (p *PubRecvPacket) WriteTo(w BufferWriter) error
WriteTo encode PubRecvPacket into buffer
type PubRelPacket ¶
type PubRelPacket struct {
PacketID uint16
}
PubRelPacket is the response to a PubRecvPacket. It is the third packet of the QoS 2 protocol exchange.
func (*PubRelPacket) Type ¶
func (p *PubRelPacket) Type() CtrlType
Type PubRelPacket's type is CtrlPubRel
func (*PubRelPacket) WriteTo ¶
func (p *PubRelPacket) WriteTo(w BufferWriter) error
WriteTo encode PubRelPacket into buffer
type PublishPacket ¶
type PublishPacket struct { IsDup bool Qos QosLevel IsRetain bool TopicName string Payload []byte PacketID uint16 }
PublishPacket is sent from a Client to a Server or from Server to a Client to transport an Application Message.
func (*PublishPacket) Type ¶
func (p *PublishPacket) Type() CtrlType
Type PublishPacket's type is CtrlPublish
func (*PublishPacket) WriteTo ¶
func (p *PublishPacket) WriteTo(w BufferWriter) error
WriteTo encode PublishPacket into buffer
type RegexRouter ¶
type RegexRouter struct {
// contains filtered or unexported fields
}
RegexRouter use regex to match topic messages
func (*RegexRouter) Dispatch ¶
func (r *RegexRouter) Dispatch(p *PublishPacket)
Dispatch the received packet
func (*RegexRouter) Handle ¶
func (r *RegexRouter) Handle(topicRegex string, h TopicHandler)
Handle will register the topic with handler
type StandardRouter ¶
type StandardRouter struct {
// contains filtered or unexported fields
}
StandardRouter implements standard MQTT routing behaviour
func NewStandardRouter ¶
func NewStandardRouter() *StandardRouter
NewStandardRouter will create a standard mqtt router
func (*StandardRouter) Dispatch ¶
func (s *StandardRouter) Dispatch(p *PublishPacket)
Dispatch defines the action to dispatch published packet
func (*StandardRouter) Handle ¶
func (s *StandardRouter) Handle(topic string, h TopicHandler)
Handle defines how to register topic with handler
type SubAckCode ¶
type SubAckCode = byte
SubAckCode is returned by server in SubAckPacket
const ( // SubOkMaxQos0 QoS 0 is used by server SubOkMaxQos0 SubAckCode = iota // SubOkMaxQos1 QoS 1 is used by server SubOkMaxQos1 // SubOkMaxQos2 QoS 2 is used by server SubOkMaxQos2 // SubFail means that subscription is not successful SubFail SubAckCode = 0x80 )
type SubAckPacket ¶
type SubAckPacket struct { PacketID uint16 Codes []SubAckCode }
SubAckPacket is sent by the Server to the Client to confirm receipt and processing of a SubscribePacket.
SubAckPacket contains a list of return codes, that specify the maximum QoS level that was granted in each Subscription that was requested by the SubscribePacket.
func (*SubAckPacket) Type ¶
func (s *SubAckPacket) Type() CtrlType
Type SubAckPacket'strategy type is CtrlSubAck
func (*SubAckPacket) WriteTo ¶
func (s *SubAckPacket) WriteTo(w BufferWriter) error
WriteTo encode SubAckPacket into buffer
type SubHandler ¶
SubHandler handles the error occurred when subscribe some topic if err is not nil, that means a error occurred when sending sub msg
type SubscribePacket ¶
SubscribePacket is sent from the Client to the Server to create one or more Subscriptions.
Each Subscription registers a Client’strategy interest in one or more TopicNames. The Server sends PublishPackets to the Client in order to forward Application Messages that were published to TopicNames that match these Subscriptions. The SubscribePacket also specifies (for each Subscription) the maximum QoS with which the Server can send Application Messages to the Client
func (*SubscribePacket) Type ¶
func (s *SubscribePacket) Type() CtrlType
Type SubscribePacket'strategy type is CtrlSubscribe
func (*SubscribePacket) WriteTo ¶
func (s *SubscribePacket) WriteTo(w BufferWriter) error
WriteTo encode SubscribePacket into buffer
type TextRouter ¶
type TextRouter struct {
// contains filtered or unexported fields
}
TextRouter uses plain string comparison to dispatch topic message this is the default router in client
func (*TextRouter) Dispatch ¶
func (r *TextRouter) Dispatch(p *PublishPacket)
Dispatch the received packet
func (*TextRouter) Handle ¶
func (r *TextRouter) Handle(topic string, h TopicHandler)
Handle will register the topic with handler
type TopicHandler ¶
TopicHandler handles topic sub message topic is the client user provided topic code can be SubOkMaxQos0, SubOkMaxQos1, SubOkMaxQos2, SubFail
type TopicRouter ¶
type TopicRouter interface { // Name is the name of router Name() string // Handle defines how to register topic with handler Handle(topic string, h TopicHandler) // Dispatch defines the action to dispatch published packet Dispatch(p *PublishPacket) }
TopicRouter defines how to route the topic message to handler
type UnSubAckPacket ¶
type UnSubAckPacket struct {
PacketID uint16
}
UnSubAckPacket is sent by the Server to the Client to confirm receipt of an UnSubPacket
func (*UnSubAckPacket) Type ¶
func (s *UnSubAckPacket) Type() CtrlType
Type UnSubAckPacket'strategy type is CtrlUnSubAck
func (*UnSubAckPacket) WriteTo ¶
func (s *UnSubAckPacket) WriteTo(w BufferWriter) error
WriteTo encode UnSubAckPacket into buffer
type UnSubHandler ¶
UnSubHandler handles the error occurred when publish some message
type UnSubPacket ¶
UnSubPacket is sent by the Client to the Server, to unsubscribe from topics.
func (*UnSubPacket) Type ¶
func (s *UnSubPacket) Type() CtrlType
Type UnSubPacket'strategy type is CtrlUnSub
func (*UnSubPacket) WriteTo ¶
func (s *UnSubPacket) WriteTo(w BufferWriter) error
WriteTo encode UnSubPacket into buffer