Documentation
¶
Index ¶
- Variables
- func ClearRetainedMessage(url string, topic string) error
- func ClearSession(url string, clientID string) error
- func SessionTest(t *testing.T, builder func() Session)
- type Callback
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect(urlString string, opts *Options) (*ConnectFuture, error)
- func (c *Client) Disconnect(timeout ...time.Duration) error
- func (c *Client) Publish(topic string, payload []byte, qos uint8, retain bool) (*PublishFuture, error)
- func (c *Client) PublishMessage(msg *packet.Message) (*PublishFuture, error)
- func (c *Client) Subscribe(topic string, qos uint8) (*SubscribeFuture, error)
- func (c *Client) SubscribeMultiple(subscriptions []packet.Subscription) (*SubscribeFuture, error)
- func (c *Client) Unsubscribe(topic string) (*UnsubscribeFuture, error)
- func (c *Client) UnsubscribeMultiple(topics []string) (*UnsubscribeFuture, error)
- type ConnectFuture
- type Future
- type Logger
- type MemorySession
- func (s *MemorySession) AllPackets(direction string) ([]packet.Packet, error)
- func (s *MemorySession) DeletePacket(direction string, id uint16) error
- func (s *MemorySession) LookupPacket(direction string, id uint16) (packet.Packet, error)
- func (s *MemorySession) PacketID() uint16
- func (s *MemorySession) Reset() error
- func (s *MemorySession) SavePacket(direction string, pkt packet.Packet) error
- type Message
- type Offline
- type Online
- type Options
- type PublishFuture
- type Service
- func (s *Service) Publish(topic string, payload []byte, qos uint8, retain bool) *PublishFuture
- func (s *Service) Start(url string, opts *Options)
- func (s *Service) Stop(clearFutures bool)
- func (s *Service) Subscribe(topic string, qos uint8, requeue bool) *SubscribeFuture
- func (s *Service) SubscribeMultiple(subscriptions []packet.Subscription, requeue bool) *SubscribeFuture
- func (s *Service) Unsubscribe(topic string, requeue bool) *UnsubscribeFuture
- func (s *Service) UnsubscribeMultiple(topics []string, requeue bool) *UnsubscribeFuture
- type Session
- type SubscribeFuture
- type UnsubscribeFuture
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClientAlreadyConnecting = errors.New("client already connecting")
ErrClientAlreadyConnecting is returned by Connect if there has been already a connection attempt.
var ErrClientConnectionDenied = errors.New("client connection denied")
ErrClientConnectionDenied is returned in the Callback if the connection has been reject by the broker.
var ErrClientExpectedConnack = errors.New("client expected connack")
ErrClientExpectedConnack is returned when the first received packet is not a ConnackPacket.
var ErrClientMissingID = errors.New("client missing id")
ErrClientMissingID is returned by Connect if no ClientID has been provided in the options while requesting to resume a session.
var ErrClientMissingPong = errors.New("client missing pong")
ErrClientMissingPong is returned in the Callback if the broker did not respond in time to a PingreqPacket.
var ErrClientNotConnected = errors.New("client not connected")
ErrClientNotConnected is returned by Publish, Subscribe and Unsubscribe if the client is not currently connected.
var ErrClientUnexpectedClose = errors.New("client unexpected close")
ErrClientUnexpectedClose is returned in the Callback if the broker closed the connection without receiving a DisconnectPacket from the client.
var ErrFutureCanceled = errors.New("future canceled")
ErrFutureCanceled is returned by Wait if the future gets canceled while waiting.
var ErrFutureTimeout = errors.New("future timeout")
ErrFutureTimeout is returned by Wait if the specified timeout is exceeded.
Functions ¶
func ClearRetainedMessage ¶ added in v0.2.0
ClearRetainedMessage will connect/disconnect and send an empty retained message. This is useful in situations where its not clear if a message has already been retained.
func ClearSession ¶
ClearSession will connect/disconnect once with a clean session request to force the broker to reset the clients session. This is useful in situations where its not clear in what state the last session was left.
func SessionTest ¶ added in v0.2.0
SessionTest will test a Session implementation. The passed builder callback should always return a fresh instances of the Session.
Types ¶
type Callback ¶
A Callback is a function called by the client upon received messages or internal errors.
type Client ¶
type Client struct {
Session Session
Callback Callback
Logger Logger
// contains filtered or unexported fields
}
A Client connects to a broker and handles the transmission of packets. It will automatically send PingreqPackets to keep the connection alive. Outgoing publish related packets will be stored in session and resend when the connection gets closed abruptly. All methods return Futures that get completed when the packets get acknowledged by the broker. Once the connection is closed all waiting futures get canceled.
Note: If clean session is false and there are packets in the store, messages might get completed after connecting without triggering any futures to complete.
Example ¶
done := make(chan struct{})
c := New()
c.Callback = func(msg *packet.Message, err error) {
if err != nil {
panic(err)
}
fmt.Printf("%s: %s\n", msg.Topic, msg.Payload)
close(done)
}
options := NewOptions()
options.ClientID = "gomqtt/client"
connectFuture, err := c.Connect("mqtt://try:try@broker.shiftr.io", options)
if err != nil {
panic(err)
}
err = connectFuture.Wait(10 * time.Second)
if err != nil {
panic(err)
}
subscribeFuture, err := c.Subscribe("test", 0)
if err != nil {
panic(err)
}
err = subscribeFuture.Wait(10 * time.Second)
if err != nil {
panic(err)
}
publishFuture, err := c.Publish("test", []byte("test"), 0, false)
if err != nil {
panic(err)
}
err = publishFuture.Wait(10 * time.Second)
if err != nil {
panic(err)
}
<-done
err = c.Disconnect()
if err != nil {
panic(err)
}
Output: test: test
func (*Client) Close ¶
Close closes the client immediately without sending a DisconnectPacket and waiting for outgoing transmissions to finish.
func (*Client) Connect ¶
func (c *Client) Connect(urlString string, opts *Options) (*ConnectFuture, error)
Connect opens the connection to the broker and sends a ConnectPacket. It will return a ConnectFuture that gets completed once a ConnackPacket has been received. If the ConnectPacket couldn't be transmitted it will return an error.
func (*Client) Disconnect ¶
Disconnect will send a DisconnectPacket and close the connection.
func (*Client) Publish ¶
func (c *Client) Publish(topic string, payload []byte, qos uint8, retain bool) (*PublishFuture, error)
Publish will send a PublishPacket containing the passed parameters. It will return a PublishFuture that gets completed once the quality of service flow has been completed.
func (*Client) PublishMessage ¶
func (c *Client) PublishMessage(msg *packet.Message) (*PublishFuture, error)
PublishMessage will send a PublishPacket containing the passed message. It will return a PublishFuture that gets completed once the quality of service flow has been completed.
func (*Client) Subscribe ¶
func (c *Client) Subscribe(topic string, qos uint8) (*SubscribeFuture, error)
Subscribe will send a SubscribePacket containing one topic to subscribe. It will return a SubscribeFuture that gets completed once a SubackPacket has been received.
func (*Client) SubscribeMultiple ¶
func (c *Client) SubscribeMultiple(subscriptions []packet.Subscription) (*SubscribeFuture, error)
SubscribeMultiple will send a SubscribePacket containing multiple topics to subscribe. It will return a SubscribeFuture that gets completed once a SubackPacket has been received.
func (*Client) Unsubscribe ¶
func (c *Client) Unsubscribe(topic string) (*UnsubscribeFuture, error)
Unsubscribe will send a UnsubscribePacket containing one topic to unsubscribe. It will return a UnsubscribeFuture that gets completed once a UnsubackPacket has been received.
func (*Client) UnsubscribeMultiple ¶
func (c *Client) UnsubscribeMultiple(topics []string) (*UnsubscribeFuture, error)
UnsubscribeMultiple will send a UnsubscribePacket containing multiple topics to unsubscribe. It will return a UnsubscribeFuture that gets completed once a UnsubackPacket has been received.
type ConnectFuture ¶
type ConnectFuture struct {
SessionPresent bool
ReturnCode packet.ConnackCode
// contains filtered or unexported fields
}
The ConnectFuture is returned by the Client on Connect.
type Future ¶
type Future interface {
// Wait will block until the future is completed or canceled. It will return
// ErrCanceled if the future gets canceled. If a timeout is specified it
// might return a ErrTimeoutExceeded.
Wait(timeout ...time.Duration) error
// Call calls the supplied callback in a separate goroutine when Wait returns.
Call(func(err error), ...time.Duration)
// contains filtered or unexported methods
}
A Future represents information that might become available in the future.
type Logger ¶
type Logger func(msg string)
A Logger is a function called by the client to log activity.
type MemorySession ¶ added in v0.2.0
type MemorySession struct {
// contains filtered or unexported fields
}
A MemorySession stores packets in memory.
func NewMemorySession ¶ added in v0.2.0
func NewMemorySession() *MemorySession
NewMemorySession returns a new MemorySession.
func (*MemorySession) AllPackets ¶ added in v0.2.0
func (s *MemorySession) AllPackets(direction string) ([]packet.Packet, error)
AllPackets will return all packets currently saved in the session.
func (*MemorySession) DeletePacket ¶ added in v0.2.0
func (s *MemorySession) DeletePacket(direction string, id uint16) error
DeletePacket will remove a packet from the session. The method must not return an error if no packet with the specified id does exists.
func (*MemorySession) LookupPacket ¶ added in v0.2.0
LookupPacket will retrieve a packet from the session using a packet id.
func (*MemorySession) PacketID ¶ added in v0.2.0
func (s *MemorySession) PacketID() uint16
PacketID will return the next id for outgoing packets.
func (*MemorySession) Reset ¶ added in v0.2.0
func (s *MemorySession) Reset() error
Reset will completely reset the session.
func (*MemorySession) SavePacket ¶ added in v0.2.0
func (s *MemorySession) SavePacket(direction string, pkt packet.Packet) error
SavePacket will store a packet in the session. An eventual existing packet with the same id gets quietly overwritten.
type Offline ¶
type Offline func()
Offline is a function that is called when the service is disconnected.
type Online ¶
type Online func(resumed bool)
Online is a function that is called when the service is connected.
type PublishFuture ¶
type PublishFuture struct {
// contains filtered or unexported fields
}
The PublishFuture is returned by the Client on Publish.
type Service ¶
type Service struct {
Session Session
Online Online
Message Message
Offline Offline
Logger Logger
MinReconnectDelay time.Duration
MaxReconnectDelay time.Duration
ConnectTimeout time.Duration
DisconnectTimeout time.Duration
// contains filtered or unexported fields
}
Service is an abstraction for Client that provides a stable interface to the application, while it automatically connects and reconnects clients in the background. Errors are not returned but logged using the Logger callback. All methods return Futures that get completed once the acknowledgements are received. Once the services is stopped all waiting futures get canceled.
Note: If clean session is false and there are packets in the store, messages might get completed after starting without triggering any futures to complete.
Example ¶
wait := make(chan struct{})
done := make(chan struct{})
options := NewOptions()
options.ClientID = "gomqtt/service"
s := NewService()
s.Online = func(resumed bool) {
fmt.Println("online!")
fmt.Printf("resumed: %v\n", resumed)
}
s.Offline = func() {
fmt.Println("offline!")
close(done)
}
s.Message = func(msg *packet.Message) {
fmt.Printf("message: %s - %s\n", msg.Topic, msg.Payload)
close(wait)
}
ClearSession("mqtt://try:try@broker.shiftr.io", "gomqtt/service")
s.Start("mqtt://try:try@broker.shiftr.io", options)
s.Subscribe("test", 0, false).Wait()
s.Publish("test", []byte("test"), 0, false)
<-wait
s.Stop(true)
<-done
Output: online! resumed: false message: test - test offline!
func (*Service) Publish ¶
Publish will send a PublishPacket containing the passed parameters. It will return a PublishFuture that gets completed once the quality of service flow has been completed.
func (*Service) Start ¶
Start will start the service with the specified configuration. From now on the service will automatically reconnect on any error until Stop is called.
func (*Service) Stop ¶
Stop will disconnect the client if online and cancel all futures if requested. After the service is stopped in can be started again.
Note: You should clear the futures on the last shutdown before exiting to ensure that all goroutines return that wait on futures.
func (*Service) Subscribe ¶
func (s *Service) Subscribe(topic string, qos uint8, requeue bool) *SubscribeFuture
Subscribe will send a SubscribePacket containing one topic to subscribe. If requeue is set to true the packet will be retried on an error.
func (*Service) SubscribeMultiple ¶
func (s *Service) SubscribeMultiple(subscriptions []packet.Subscription, requeue bool) *SubscribeFuture
SubscribeMultiple will send a SubscribePacket containing multiple topics to subscribe. If requeue is set to true the packet will be retried on an error.
func (*Service) Unsubscribe ¶
func (s *Service) Unsubscribe(topic string, requeue bool) *UnsubscribeFuture
Unsubscribe will send a UnsubscribePacket containing one topic to unsubscribe. If requeue is set to true the packet will be retried on an error.
func (*Service) UnsubscribeMultiple ¶
func (s *Service) UnsubscribeMultiple(topics []string, requeue bool) *UnsubscribeFuture
UnsubscribeMultiple will send a UnsubscribePacket containing multiple topics to unsubscribe. If requeue is set to true the packet will be retried on an error.
type Session ¶ added in v0.2.0
type Session interface {
// PacketID will return the next id for outgoing packets.
PacketID() uint16
// SavePacket will store a packet in the session. An eventual existing
// packet with the same id gets quietly overwritten.
SavePacket(direction string, pkt packet.Packet) error
// LookupPacket will retrieve a packet from the session using a packet id.
LookupPacket(direction string, id uint16) (packet.Packet, error)
// DeletePacket will remove a packet from the session. The method must not
// return an error if no packet with the specified id does exists.
DeletePacket(direction string, id uint16) error
// AllPackets will return all packets currently saved in the session.
AllPackets(direction string) ([]packet.Packet, error)
// Reset will completely reset the session.
Reset() error
}
A Session is used to persist incoming and outgoing packets.
type SubscribeFuture ¶
type SubscribeFuture struct {
ReturnCodes []uint8
// contains filtered or unexported fields
}
The SubscribeFuture is returned by the Client on Subscribe.
type UnsubscribeFuture ¶
type UnsubscribeFuture struct {
// contains filtered or unexported fields
}
UnsubscribeFuture is returned by the Client on Unsubscribe.