client

package
v0.0.0-...-3133fea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func (c *Client) Disconnect() (err error)

Disconnect sends a disconnect packet to the server and closes the connection.

func (*Client) Ping

func (c *Client) Ping() error

Ping sends a ping packet to the server and blocks for a response.

func (*Client) Publish

func (c *Client) Publish(
	topic mqtt.Topic,
	payload []byte,
	options ...*PublishOptions,
) error

Publish publishes a new packet to the specified topic.

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

func (c *Client) Unsubscribe(topicNames ...string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL