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/energostack/bisquitt/client" "github.com/energostack/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 ¶
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect() error
- func (c *Client) Dial(address string) error
- func (c *Client) Disconnect() error
- func (c *Client) Ping() error
- func (c *Client) Publish(topic string, payload []byte, qos uint8, retain bool) error
- func (c *Client) PublishPredefined(topicID uint16, payload []byte, qos uint8, retain bool) error
- func (c *Client) Register(topic string) error
- func (c *Client) Sleep(duration time.Duration) error
- func (c *Client) Subscribe(topic string, qos uint8, callback MessageHandlerFunc) error
- func (c *Client) SubscribePredefined(topicID uint16, qos uint8, callback MessageHandlerFunc) error
- func (c *Client) Unsubscribe(topic string) error
- func (c *Client) UnsubscribePredefined(topicID uint16) error
- func (c *Client) Wait() error
- type ClientConfig
- type MessageHandlerFunc
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 ¶
Close closes the connection with the MQTT-SN gateway. The client sends a DISCONNECT packet before closing the connection.
func (*Client) Connect ¶
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 ¶
Dial connects to a MQTT-SN broker. The address must be in the "host:port" form.
func (*Client) Disconnect ¶
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) PublishPredefined ¶
PublishPredefined publishes a message to the provided predefined topic.
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 ¶
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 ¶
Unsubscribe unsubscribes from a topic. If the topic is 2 characters long, it's treated as a short topic.
func (*Client) UnsubscribePredefined ¶
UnsubscribePredefined unsubscribes from a predefined topic.
type ClientConfig ¶
type ClientConfig struct { // UsePSK controls whether pre-shared key should be used to secure the // connection to the MQTT-SN gateway. If UsePSK is true, you must provide // PSKIdentityHint, PSKAPIBasicAuthUsername, PSKAPIBasicAuthPassword and // PSKAPIEndpoint. // If UsePSK is true, the client will use PSKKeys instead of the certificate // and private key. UsePSK bool PSKKeys *cache.Cache PSKCacheExpiration time.Duration PSKIdentityHint string PSKAPITimeout time.Duration PSKAPIBasicAuthUsername string PSKAPIBasicAuthPassword string PSKAPIEndpoint string // 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 }
Source Files ¶
- broker_publish_qos2_transaction.go
- client.go
- connect_transaction.go
- disconnect_transaction.go
- message_handlers.go
- net.go
- ping_transaction.go
- publish_qos1_transaction.go
- publish_qos2_transaction.go
- register_transaction.go
- sleep_transaction.go
- subscribe_transaction.go
- transaction.go
- unsubscribe_transaction.go