Documentation ¶
Index ¶
- Variables
- type Client
- func (c *Client) Connect(options ...*ConnectOptions) error
- func (c *Client) Disconnect() (err error)
- func (c *Client) Ping() error
- func (c *Client) Publish(topic mqtt.Topic, payload []byte, options ...*PublishOptions) error
- func (c *Client) Subscribe(topics ...mqtt.Subscription) ([]uint8, error)
- func (c *Client) Unsubscribe(topicNames ...string) error
- type ClientOptions
- type ConnectOptions
- func (opts *ConnectOptions) SetCleanSession(cleanSession bool)
- func (opts *ConnectOptions) SetKeepAlive(duration time.Duration)
- func (opts *ConnectOptions) SetPassword(password string)
- func (opts *ConnectOptions) SetUsername(username string)
- func (opts *ConnectOptions) SetWillMessage(message []byte)
- func (opts *ConnectOptions) SetWillTopic(topic mqtt.Topic, retain bool)
- type PublishOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrIllegalResponse is an internal error returned if the client // receives an illegal packet. ErrIllegalResponse = fmt.Errorf("illegal response received from server") // ErrInternalConflict is a similar internal error returned if the // internal receive routine sends an unexpected packet to the main // routine. ErrInternalConflict = fmt.Errorf("received unexpected packet") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // ClientID is the identity communicated with the server on connect. ClientID string // contains filtered or unexported fields }
Client is the package representation of an MQTT client. The struct holds all internal client state and session data to provide a functional high-level API to the MQTT protocol.
func NewClient ¶
func NewClient(connection net.Conn, options ...*ClientOptions) (client *Client)
NewClient initialize a new MQTT client with the given configuration and connection. After initializing the client, the user MUST call Connect before using the rest of the client API. Upon calling Connect, the client takes complete ownership of the connection and any reads or writes to the connection will lead to the client throwing an error.
func (*Client) Connect ¶
func (c *Client) Connect(options ...*ConnectOptions) error
Connect establishes connection to the mqtt broker.
func (*Client) Disconnect ¶
Disconnect sends a disconnect packet to the server and closes the connection.
func (*Client) Subscribe ¶
func (c *Client) Subscribe(topics ...mqtt.Subscription) ([]uint8, error)
Subscribe sends a subscribe request with the given topics. On success the list of status codes corresponding to the provided topics are returned.
func (*Client) Unsubscribe ¶
Unsubscribe sends an unsubscribe packet to the topic names. The client will no longer receive packets on the given topics.
type ClientOptions ¶
type ClientOptions struct { // Version signifies the protocol version to be used. Defaults to 3.1.1 Version *mqtt.Version // The client identity communicated with the server. Defaults to random // UUID (version 4). ClientID *string // Timeout sets the duration for how long the client blocks on requests. Timeout *time.Duration }
ClientOptions holds configuration options to initialize a new Client.
func NewClientOptions ¶
func NewClientOptions() *ClientOptions
NewClientOptions initializes a new empty client options struct.
func (*ClientOptions) SetClientID ¶
func (opts *ClientOptions) SetClientID(id string)
SetClientID sets the client id communicated with the server.
func (*ClientOptions) SetTimeout ¶
func (opts *ClientOptions) SetTimeout(timeout time.Duration)
SetTimeout sets the timeout duration for blocking on send and receive to the connection. If unset, the client blocks indefinitely.
func (*ClientOptions) SetVersion ¶
func (opts *ClientOptions) SetVersion(version mqtt.Version)
SetVersion sets the protocol version used by this client.
type ConnectOptions ¶
type ConnectOptions struct { // CleanSession indicates whether the server should discard any // previously stored session-state for the client. CleanSession *bool // KeepAlive is the number of seconds the session is active, defaults // to 0 ("infinite"). KeepAlive *uint16 // Username MQTT credentials. (Defaults to none) Username *string // Password MQTT credentials. (Defaults to none) // NOTE: If Password is set Username MUST also be set. Password *string // WillTopic is the topic to publish when connection is established. // Defaults to none. WillTopic *mqtt.Topic // WillMessage is the payload to the will topic that will be published // on connect. Defaults to empty. WillMessage []byte // WillRetain determines whether the server should retain the packet // for inbound subscribers for the lifetime of the session. // NOTE: if the WillTopic QoS is QoS0 the server may discard the packet // at any time. WillRetain *bool }
ConnectOptions holds configuration options for making a connect request.
func NewConnectOptions ¶
func NewConnectOptions() *ConnectOptions
NewConnectOptions initializes a new connect options struct.
func (*ConnectOptions) SetCleanSession ¶
func (opts *ConnectOptions) SetCleanSession(cleanSession bool)
SetCleanSession sets the clean-session flag.
func (*ConnectOptions) SetKeepAlive ¶
func (opts *ConnectOptions) SetKeepAlive(duration time.Duration)
SetKeepAlive sets the keep alive to the given duration. NOTE: If the duration is longer than the maximum 18:12:15 (hr:min:sec), the value will be truncated to this maximum.
func (*ConnectOptions) SetPassword ¶
func (opts *ConnectOptions) SetPassword(password string)
SetPassword sets the password credential.
func (*ConnectOptions) SetUsername ¶
func (opts *ConnectOptions) SetUsername(username string)
SetUsername sets the username credential.
func (*ConnectOptions) SetWillMessage ¶
func (opts *ConnectOptions) SetWillMessage(message []byte)
SetWillMessage sets the will message payload to the given buffer.
func (*ConnectOptions) SetWillTopic ¶
func (opts *ConnectOptions) SetWillTopic(topic mqtt.Topic, retain bool)
SetWillTopic sets the will topic to publish on connect.
type PublishOptions ¶
type PublishOptions struct { // Retain determines whether the server should retain the application // message and it's QoS to be delivered to future subscribers. Retain *bool }
PublishOptions contains configuration options for making a publish request.
func NewPublishOptions ¶
func NewPublishOptions() *PublishOptions
NewPublishOptions initializes a new blank publish options struct.
func (*PublishOptions) SetRetain ¶
func (opts *PublishOptions) SetRetain(retain bool)
SetRetain sets the retain flag to the given value.