aliyun_mqtt_go

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 12 Imported by: 0

README

aliyun_mqtt_go

MQTT connection auxiliary package based on Aliyun

Usage

Installation
go get github.com/zhangsq-ax/aliyun_mqtt_go
Example

Publish message

package main

import (
  "github.com/zhangsq-ax/aliyun_mqtt_go"
  "github.com/zhangsq-ax/aliyun_mqtt_go/constants"
  "github.com/zhangsq-ax/aliyun_mqtt_go/options"
)

func main() {
  // Get MQTT client
  client, err := aliyun_mqtt_go.NewMQTTClient(&options.MQTTClientOptions{
    AuthType:   constants.AuthTypeSign,
		Protocol:   constants.ConnectProtocolSsl,
		InstanceID: "xxxx-xx-xxxxxxxxxxx",
		Endpoints:  []string{"xxxx-xx-xxxxxxxxxx.mqtt.aliyuncs.com"},
		Username:   "xxxxxxxxxxxxxxxxx",
		Password:   "xxxxxxxxxxxxxxxxx",
		GroupID:        "GID_xxxx",
		ClientID:       "xxxxxxxxxx",
  })
  if err != nil {
    panic(err)
  }
  
  // Publish message
  err = client.Publish(&options.PublishOptions{
    Topic: "test",
    Qos: 2,
    Payload: "hello mqtt",
  })
  if err != nil {
    panic(err)
  }
}

Subscribe Message

package main

import (
  "fmt"
  "github.com/zhangsq-ax/aliyun_mqtt_go"
  "github.com/zhangsq-ax/aliyun_mqtt_go/constants"
  "github.com/zhangsq-ax/aliyun_mqtt_go/options"
)

func main() {
  // Get MQTT client
  client, err := aliyun_mqtt_go.NewMQTTClient(&options.MQTTClientOptions{
    AuthType:   constants.AuthTypeSign,
		Protocol:   constants.ConnectProtocolSsl,
		InstanceID: "xxxx-xx-xxxxxxxxxxx",
		Endpoints:  []string{"xxxx-xx-xxxxxxxxxx.mqtt.aliyuncs.com"},
		Username:   "xxxxxxxxxxxxxxxxx",
		Password:   "xxxxxxxxxxxxxxxxx",
		GroupID:        "GID_xxxx",
		ClientID:       "xxxxxxxxxx",
  })
  if err != nil {
    panic(err)
  }
  
  // Subscribe message
  msgChan, err := client.Subscribe(&options.SubscribeOptions{
    Topic: "test",
    Qos: 2,
  })
  if err != nil {
    panic(err)
  }
  
  for msg := range msgChan {
    fmt.Println(msg.Payload())
  }
}

Documentation

Index

Constants

View Source
const TokenCheckCycle = 15 // token 检查周期,单位:s

Variables

This section is empty.

Functions

func GetClient added in v1.1.0

func GetClient(opts *options.MQTTClientOptions) (client *mqtt.Client, expiredTime int64, err error)

GetClient 获取 MQTT 客户端对象

Types

type ConnectHelper

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

ConnectHelper MQTT 客户端连接助手

func NewConnectHelperFromClientOptions added in v1.1.0

func NewConnectHelperFromClientOptions(opts *options.MQTTClientOptions) *ConnectHelper

NewConnectHelperFromClientOptions 根据 ClientOptions 创建 ConnectHelper 实例

func (*ConnectHelper) GetClient

func (helper *ConnectHelper) GetClient() (client *mqtt.Client, expiredTime int64, err error)

GetClient 获取 MQTT 客户端,客户端处理未连接状态,需要手动连接

type MQTTClient added in v1.1.0

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

MQTTClient MQTT 操作对象

func NewMQTTClient added in v1.1.0

func NewMQTTClient(opts *options.MQTTClientOptions) (*MQTTClient, error)

NewMQTTClient 创建新的 MQTT 客户端对象

func (*MQTTClient) Publish added in v1.1.0

func (c *MQTTClient) Publish(opts *options.PublishOptions) error

Publish 向 MQTT 服务发布消息

func (*MQTTClient) Subscribe added in v1.1.0

func (c *MQTTClient) Subscribe(opts *options.SubscribeOptions) (chan *mqtt.Message, error)

Subscribe 从 MQTT 服务订阅消息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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