gomqtt

package module
v0.0.0-...-f03b72d Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: MIT Imports: 3 Imported by: 0

README

gomqtt


Requirements

gomqtt library requires Go version >=1.14

拉取代码

go get -u github.com/jellycheng/gomqtt
    或者
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/jellycheng/gomqtt

直接拉取master分支代码:
    go get -u github.com/jellycheng/gomqtt@master

发布消息

package main

import (
	"fmt"
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"github.com/jellycheng/gomqtt"
	"time"
)

func main() {
	// 连接的回调
	var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
		fmt.Println("连接成功")
	}
	// 连接丢失的回调
	var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
		fmt.Printf("连接丢失Connect lost: %v", err)
	}

	cfg1 := gomqtt.NewMqttConfig("tcp://broker.emqx.io:1883",
		gomqtt.WithMqttConfigClientID("go_mqtt_client_pub"))
	opts := cfg1.GetClientOptions()
	opts = cfg1.GetClientOptions()
	opts = cfg1.GetClientOptions()
	opts.OnConnect = connectHandler            //连接的回调
	opts.OnConnectionLost = connectLostHandler //连接丢失的回调
	client, err := gomqtt.NewMqttClient(opts)
	if err != nil {
		fmt.Println("new client error")
		return
	}

	num := 10
	for i := 0; i < num; i++ {
		text := fmt.Sprintf("发送消息信息: %d", i)
		err := gomqtt.Publish(client, "topic/test", gomqtt.QoS0, false, text)
		if err != nil {
			fmt.Println("消息发送失败:", err.Error())
		} else {
			fmt.Println("发送成功:", text)
		}
		time.Sleep(time.Second)
	}

}

订阅消息

package main

import (
	"fmt"
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"github.com/jellycheng/gomqtt"
	"time"
)

func main() {
	// 连接的回调
	var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
		fmt.Println("连接成功")
	}
	// 连接丢失的回调
	var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
		fmt.Printf("连接丢失Connect lost: %v", err)
	}
	// 全局 MQTT 消息处理
	var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
		fmt.Printf("接收消息: %s from topic: %s\n", msg.Payload(), msg.Topic())
	}

	cfg1 := gomqtt.NewMqttConfig("tcp://broker.emqx.io:1883",
		gomqtt.WithMqttConfigClientID("go_mqtt_client_sub01"))
	opts := cfg1.GetClientOptions()
	opts.OnConnect = connectHandler            //连接的回调
	opts.OnConnectionLost = connectLostHandler //连接丢失的回调
	opts.SetDefaultPublishHandler(messagePubHandler)
	client, err := gomqtt.NewMqttClient(opts)
	if err != nil {
		fmt.Println("new client error")
		return
	}
	// 订阅消息
	gomqtt.Subscribe(client, "topic/test", gomqtt.QoS1, nil)

	time.Sleep(5 * time.Minute)

}

Documentation

Index

Constants

View Source
const (
	QoS0 byte = 0 //最多一次,即:<=1,消息可能丢失
	QoS1 byte = 1 //至少一次,即:>=1,消息不会丢失,但可能重复
	QoS2 byte = 2 //一次,即:=1,消息不会丢失,也不会重复
)

Variables

This section is empty.

Functions

func NewMqttClient

func NewMqttClient(opts *mqtt.ClientOptions) (mqtt.Client, error)

func Publish

func Publish(client mqtt.Client, topic string, qos byte, retained bool, payload interface{}) error

Publish 发消息

func Subscribe

func Subscribe(client mqtt.Client, topic string, qos byte, callback mqtt.MessageHandler) error

Subscribe 订阅消息/消费消息

Types

type MqttConfig

type MqttConfig struct {
	Broker   string //mqtt地址,示例:tcp://broker.emqx.io:1883
	Username string //账号
	Pwd      string //密码
	ClientID string //客户端id
	// contains filtered or unexported fields
}

func NewMqttConfig

func NewMqttConfig(broker string, opts ...MqttConfigOption) *MqttConfig

func (*MqttConfig) GetClientOptions

func (m *MqttConfig) GetClientOptions() *mqtt.ClientOptions

type MqttConfigManage

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

func NewMqttConfigManage

func NewMqttConfigManage() *MqttConfigManage

func (MqttConfigManage) Get

func (m MqttConfigManage) Get(group string) (*MqttConfig, error)

func (MqttConfigManage) GetAll

func (m MqttConfigManage) GetAll() map[string]*MqttConfig

func (*MqttConfigManage) Set

func (m *MqttConfigManage) Set(group string, mqttCfg *MqttConfig) *MqttConfigManage

type MqttConfigOption

type MqttConfigOption func(m *MqttConfig)

func WithMqttConfigBroker

func WithMqttConfigBroker(broker string) MqttConfigOption

func WithMqttConfigClientID

func WithMqttConfigClientID(id string) MqttConfigOption

func WithMqttConfigPwd

func WithMqttConfigPwd(pwd string) MqttConfigOption

func WithMqttConfigUsername

func WithMqttConfigUsername(username string) MqttConfigOption

Jump to

Keyboard shortcuts

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