Help us shape the future of Go! Take the 2026 Go Developer Survey

songoku

package module
v0.0.0-...-275ec38 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

README

songoku

the mqtt client by go.

demo:

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/SongLiangChen/songoku"
)

func main() {
	client := songoku.NewMqttClient()

	client.HandPublish(func(msg *songoku.MqttMsg) {
		fmt.Printf("rec new msg: %v\n", *msg)
	})

	if err := client.Connect("127.0.0.1:1883"); err != nil {
		fmt.Println(err)
		return
	}
	defer client.Disconnect()

	go client.Consume()

	client.Subscribe("topicA", 1, 0)

	time.Sleep(time.Second)
	go TestPublish()

	wg := sync.WaitGroup{}
	wg.Add(1)
	wg.Wait()
}

func TestPublish() {
	client := songoku.NewMqttClient()
	client.ID = "your_client_id"
	if err := client.Connect("127.0.0.1:1883"); err != nil {
		fmt.Println(err)
		return
	}
	defer client.Disconnect()

	go client.Consume()

	client.Publish(&songoku.MqttMsg{
		Topic:   "topicA",
		Content: "hello",
		Qos:     0,
	})

	time.Sleep(time.Second)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPid

func GetPid() int

Types

type Client

type Client struct {
	ID           string // the Client Identifier, cannot be nil, and should be unique
	Keepalive    int    // idle time, when timeout, disconnect from server
	WillRetain   bool   // "will retain" flag, it must be false unless "WillFlag == true"
	WillQos      int    // "will qos" flag, it must be zero unless "WillFlag == frue"
	WillFlag     bool   // "will flag"
	CleanSession bool

	WillTopic  string
	WillWorlds string
	UserName   string
	PassWorld  string

	sync.Mutex
	// contains filtered or unexported fields
}

func NewMqttClient

func NewMqttClient() *Client

func (*Client) Connect

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

func (*Client) Consume

func (c *Client) Consume()

it usually call by go, like "go c.Consume()"

func (*Client) Disconnect

func (c *Client) Disconnect() error

func (*Client) HandConnack

func (c *Client) HandConnack(fn func())

Will be callback when mqtt client connect to broker success

func (*Client) HandPuback

func (c *Client) HandPuback(fn func())

A PUBACK Packet is the response to a PUBLISH Packet with QoS level 1.

func (*Client) HandPubcomp

func (c *Client) HandPubcomp(fn func(pid []byte))

The PUBCOMP Packet is the response to a PUBREL Packet. It is the fourth and final packet of the QoS 2 protocol exchange.

func (*Client) HandPublish

func (c *Client) HandPublish(fn func(msg *MqttMsg))

Will be callback when mqtt msg publish success

func (*Client) HandPubrec

func (c *Client) HandPubrec(fn func(pid []byte))

A PUBREC Packet is the response to a PUBLISH Packet with QoS 2. It is the second packet of the QoS 2 protocol exchange.

func (*Client) HandPubrel

func (c *Client) HandPubrel(fn func(pid []byte))

A PUBREL Packet is the response to a PUBREC Packet. It is the third packet of the QoS 2 protocol exchange.

func (*Client) HandSuback

func (c *Client) HandSuback(fn func())

Will be callback when subscribe topic success

func (*Client) Subscribe

func (c *Client) Subscribe(topic string, pid int, qos int) error

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(topic string, pid int) error

func (*Client) Write

func (c *Client) Write(d []byte) error

type MqttMsg

type MqttMsg struct {
	Topic   string
	Content string
	Pid     int
	Dup     bool
	Qos     int
	Retain  bool
}

Jump to

Keyboard shortcuts

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