client

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: EPL-2.0 Imports: 19 Imported by: 1

Documentation

Overview

Package client implements a MQTT-SN version 1.2 client with optional DTLS encryption.

Use ClientConfig struct to set various client's options and features.

Example:

import (
	"fmt"
	"time"

	"github.com/energomonitor/bisquitt/client"
	"github.com/energomonitor/bisquitt/util"
)

func main() {
	brokerAddress := "localhost:1883"
	topic := "dev/test"
	payload := "test message"
	qos := 1
	retain := false

	clientCfg := &client.ClientConfig{
		ClientID:       "test-client",
		RetryDelay:     10 * time.Second,
		RetryCount:     4,
		ConnectTimeout: 20 * time.Second,
		KeepAlive:      60 * time.Second,
		CleanSession:   true,
	}
	logger := util.NewDebugLogger("test")
	c := client.NewClient(logger, clientCfg)

	fmt.Printf("Connecting to a MQTT-SN broker %#v\n", brokerAddress)
	if err := c.Dial(brokerAddress); err != nil {
		panic(err)
	}
	if err := c.Connect(); err != nil {
		panic(err)
	}
	defer c.Disconnect()

	fmt.Printf("Registering topic %#v\n", topic)
	if err := c.Register(topic); err != nil {
		panic(err)
	}

	fmt.Printf("Publishing: %s <- %s\n", topic, string(payload))
	if err := c.Publish(topic, uint8(qos), retain, []byte(payload)); err != nil {
		panic(err)
	}

	fmt.Println("Everything OK")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(log util.Logger, cfg *ClientConfig) *Client

NewClient sets up a new client according to the provided configuration.

func (*Client) Close

func (c *Client) Close() error

Close closes the connection with the MQTT-SN gateway. The client sends a DISCONNECT packet before closing the connection.

func (*Client) Connect

func (c *Client) Connect() error

Connect sends a CONNECT packet to the MQTT-SN gateway. According to the MQTT-SN specification, this must be the first packet the client sends unless it's a PUBLISH packet with QoS = -1.

func (*Client) Dial

func (c *Client) Dial(address string) error

Dial connects to a MQTT-SN broker. The address must be in the "host:port" form.

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect sends a DISCONNECT packet to the MQTT-SN gateway. According to the "Client's state transition diagram" in the MQTT-SN specification v. 1.2, chapter 6.14, the client can send a DISCONNECT packet only in an ACTIVE or AWAKE state. This function does not return error if the client is in other states so it's usable unconditionally in defer.

func (*Client) Ping

func (c *Client) Ping() error

Ping sends a PING packet to the MQTT-SN gateway.

func (*Client) Publish

func (c *Client) Publish(topic string, payload []byte, qos uint8, retain bool) error

Publish publishes a message to the provided topic.

func (*Client) PublishPredefined added in v0.5.0

func (c *Client) PublishPredefined(topicID uint16, payload []byte, qos uint8, retain bool) error

PublishPredefined publishes a message to the provided predefined topic.

func (*Client) Register

func (c *Client) Register(topic string) error

Register sends a REGISTER packet to the MQTT-SN gateway.

func (*Client) Sleep

func (c *Client) Sleep(duration time.Duration) error

Sleep informs the MQTT-SN gateway that the client is going to sleep.

func (*Client) Subscribe

func (c *Client) Subscribe(topic string, qos uint8, callback MessageHandlerFunc) error

Subscribe subscribes to a topic with the provided QoS. If the topic is 2 characters long, it's treated as a short topic. The received packets are passed to the provided callback.

func (*Client) SubscribePredefined added in v0.5.0

func (c *Client) SubscribePredefined(topicID uint16, qos uint8, callback MessageHandlerFunc) error

SubscribePredefined subscribes to a predefined topic with the provided QoS. The received packets are passed to the provided callback.

func (*Client) Unsubscribe added in v0.5.0

func (c *Client) Unsubscribe(topic string) error

Unsubscribe unsubscribes from a topic. If the topic is 2 characters long, it's treated as a short topic.

func (*Client) UnsubscribePredefined added in v0.5.0

func (c *Client) UnsubscribePredefined(topicID uint16) error

UnsubscribePredefined unsubscribes from a predefined topic.

func (*Client) Wait

func (c *Client) Wait() error

Wait blocks until the client is terminated.

type ClientConfig

type ClientConfig struct {
	// UseDTLS controls whether DTLS should be used to secure the connection
	// to the MQTT-SN gateway.
	UseDTLS        bool
	Certificate    *tls.Certificate
	PrivateKey     crypto.PrivateKey
	CACertificates []*x509.Certificate
	// SelfSigned controls whether the client should use a self-signed
	// certificate and key.  If SelfSigned is false and UseDTLS is true, you
	// must provide CertFile and KeyFile.
	SelfSigned bool
	// Insecure controls whether the client verifies server's certificate.
	// If Insecure is true, the client accepts any certificate and is
	// susceptible to a man-in-the-middle attack. Should be used for testing
	// only.
	Insecure bool
	ClientID string
	// User is used to authenticate with the MQTT-SN gateway.
	User string
	// Password is used to authenticate with the MQTT-SN gateway.
	Password         []byte
	CleanSession     bool
	WillTopic        string
	WillPayload      []byte
	WillQOS          uint8
	WillRetained     bool
	KeepAlive        time.Duration
	ConnectTimeout   time.Duration
	PredefinedTopics topics.PredefinedTopics
	// TRetry in MQTT-SN specification
	RetryDelay time.Duration
	// NRetry in MQTT-SN specification
	RetryCount uint
}

type MessageHandlerFunc

type MessageHandlerFunc func(client *Client, topic string, pkt *pkts1.Publish)

Subscribed message handler callback type.

Jump to

Keyboard shortcuts

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