Paho GO Client   
语言   GO
协议 EPL AND EDL
官网地址 http://www.eclipse.org/paho/
API类型 Asynchronous 

描述

Paho GO 库包含一个可以作为独立读写MQTT的包。

PAho Go 库目前是0.9版本,即将释放1.0的稳定版本,由于被商业和开源项目采用(例如Gobot),该项目被积极的维护。

特性

MQTT3.1   Qos 0
MQTT3.1.1   Qos 1
LWT   Q0s 2
SSL/TLS   Authentication
Automatic Reconnect   Throttling  

使用

安装

假设你有一个Go的开发环境,你有一个很简单的方法获取Paho Go库并运行;

1
go get git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git

 如下会将库下载到你的$GOPATH/src 目录下,你就可以在你的项目下添加到你的Import列表下使用该库:

git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git

连接

1
2
3
4
5
6
opts := mqtt.NewClientOptions().AddBroker("tcp://broker.hivemq.com:1883").SetClientID("sample")
  
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
    panic(token.Error())
}

  为了连接到MQTT代理,你必须提供两个必要的参数:代理的URL和使用的客户端ID。为此,我们创建了一个新的ClientOptions结构体实例,该结构体包含代理的Url和客户端ID。在ClientOptions结构体上操作的方法们返回一个可更改的结构体指针,这使你可以将方法连在一块。

  参考了Paho Java 库,Paho Go 库允许你在完成操作时很容易的接受一个token,该token可以被用来指示操作是否完成。token.Wait()是个阻塞函数,只有在操作完成时才返回。token.WaitTimeout()会在操作完成后等待几毫秒后返回。

连接到MQTT3.1或MQTT3.1.1

1
2
3
4
5
6
7
opts := mqtt.NewClientOptions().AddBroker("tcp://broker.hivemq.com:1883").SetClientID("sample")
opts.SetProtocolVersion(4)
  
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
    panic(token.Error())
}

  Paho Go 库默认使用MQTT3.1.1协议连接代理,如果失败他会自动回调并使用MQTT3.1协议连接。SetProtocolVersion()方法允许你明确的设置连接协议,4是3.1.1,3是3.1。如果显示设置,那么回调机制是禁用的。

使用LWT(临终遗嘱)连接

LWT:该协议提供了检测方式,利用KeepAlive机制在客户端异常断开时发现问题。因此当客户端电量耗尽、崩溃或者网络断开时,消息代理会采取相应措施。

客户端会向任意点的消息代理发送“临终遗嘱”(LWT)信息,当消息代理检测到客户端离线(连接并未关闭),就会发送保存在特定主题上的 LWT 信息,让其它客户端知道该节点已经意外离线。

1
2
3
4
5
6
7
opts := mqtt.NewClientOptions().AddBroker("tcp://broker.hivemq.com:1883").SetClientID("sample")
opts.SetWill("my/will/topic""Goodbye", 1, true)
  
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
    panic(token.Error())
}

  Paho Go库有两个方法设置LWT。SetWill()和SetBinaryWill(),这两个方法都有四个参数。两个方法中的第一个参数都是字符串型的LWT订阅。第二个参数是消息体(payload),在SetWill()中是一个字符创型,在SetBinaryWill()中是byte数组。第三个参数是消息的qos类型,第四个参数是LWT的是否保持连接布尔值。

使用用户名/密码连接

1
2
3
4
5
6
7
8
opts := mqtt.NewClientOptions().AddBroker("tcp://broker.hivemq.com:1883").SetClientID("sample")
opts.SetUsername("username")
opts.SetPassword("password")
  
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
    panic(token.Error())
}

发布

1
2
3
4
5
c.Publish("test/topic", 1, false, "Example Payload")
  
if token := c.Publish("test/topic", 1, false, "Example Payload"); token.Wait() && token.Error() != nil {
    fmt.Println(token.Error())
}

  上述中,c是mqtt.NewClient()返回的mqtt.Client。发布 使用4个参数;发布消息的字符串型的topic,消息的qos质量,是否保持消息连接的bool,或者既可以是字符串形式也可以是byte数组的消息体(payload)。并且我示范了如何使用和不适用token进行消息发布。

发布保留连接信息

1
c.Publish("test/topic", 1, true, "Example Payload")

订阅

1
2
3
4
5
6
7
var msgRcvd := func(client *mqtt.Client, message mqtt.Message) {
    fmt.Printf("Received message on topic: %s\nMessage: %s\n", message.Topic(), message.Payload())
}
  
if token := c.Subscribe("example/topic", 0, msgRcvd); token.Wait() && token.Error() != nil {
    fmt.Println(token.Error())
}

  Subscribe()使用3个参数,一个订阅的字符串形式的topic,订阅的qos质量和一个在接受到匹配订阅消息时的函数回调。回调函数必须有一个func(*mqtt.Client,mqtt.Message)的结构。当回调函数为空(nil)的时候,在库接受到消息后会调用客户端的默认消息处理程序(如果设置)。可以在结构体ClientOptions的SetDefaultPublishHandler()中设置。

取消订阅

1
2
3
4
5
c.Unsubscribe("example/topic")
  
if token := c.Unsubscribe("example/topic"); token.Wait() && token.Error() != nil {
    fmt.Println(token.Error())
}

  Unsubscribe()可以接受多于一个的取消订阅的topic的参数,每个topic使用单独的字符串型数组参数分开。

断开连接

1
c.Disconnect(250)

  Disconnect()使用一个参数,该参数为线程中结束任何工作的毫秒数。

使用SSL/TLS

1
2
3
4
5
6
opts := mqtt.NewClientOptions().AddBroker("ssl://iot.eclipse.org:8883").SetClientID("sample")
  
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
    panic(token.Error())
}

  你可以非常简单的通过改变代理URL来连接到具有SSL/TLS的代理;ssl,tls或者tcps都被client支持并且安全的连接。此处假设你连接的是使用系统已知证书的代理。如果你使用自我签名的证书你需要使用 TLS。使用ClientOptions的SetTlSConfig()配置。Paho Go库的Sample文件夹中有此示例代码。

go ---MQTT client的更多相关文章

  1. M2Mqtt is a MQTT client available for all .Net platform

    Introduction M2Mqtt is a MQTT client available for all .Net platform (.Net Framework, .Net Compact F ...

  2. python mqtt client publish操作

    使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic. #-*-coding:utf-8-*- import paho.mqtt ...

  3. mqtt client python example

    This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...

  4. mqtt client api: 阻塞API

    fusesource版本:mqtt-client-1.11.jar下载地址:https://github.com/fusesource/mqtt-client fusesource提供三种mqtt c ...

  5. MQTT Client library for C (MQTT客户端C语言库-paho)

    原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/index.html 来自我的CSDN博客   最近在使用Paho的MQTT客 ...

  6. MQTT Server搭建(apache-apollo)和MQtt Client搭建

    目标 本文就MQTT server和client搭建做以下总结,方便测试及开发使用,能基于MQTT软件发送和接收消息. 介绍 MQTT是基于tcp的消息发送,目前JAVA方面有两种实现,分别是mqtt ...

  7. php mqtt client

    <?php /* phpMQTT */ class phpMQTT { private $socket; /* holds the socket */ private $msgid = 1; / ...

  8. Asynchronous MQTT client library for C (MQTT异步客户端C语言库-paho)

    原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTAsync/html/index.html MQTT异步客户端C语言库   用于C的异步 MQTT 客 ...

  9. 一套权威的 MQTT Client 库

    主流的语言都支持,可链接到 github ,亲测golang client 简单好用 http://www.eclipse.org/paho/downloads.php

随机推荐

  1. js 加密混淆工具

    访问路径:https://www.sojson.com/javascriptobfuscator.html

  2. 【分布式搜索引擎】Elasticsearch之如何安装Elasticsearch

    在Macos上安装 一.下载安装过程 最新版本下载地址: https://www.elastic.co/cn/downloads/elasticsearch 历史版本下载地址: https://www ...

  3. 全球唯一标识符 System.Guid.NewGuid().ToString()

    System.Guid.NewGuid().ToString(); //ToString() 为 null 或空字符串 (""),则使用"D". 结果:8209 ...

  4. Centos7安装dubbo与zookeeper服务配置

    目录 环境: 第一步:安装jdk,并且配置环境变量 1.解压jdk: 2.配置环境变量: 3.保存并使文件立即生效: 4.立即重启虚拟机,进行下面的安装 第二步:安装注册中心zookeeper 1.解 ...

  5. 关于微信小程序中遇到的各种问题汇总(持续更新)

    1.关于 <input />标签容易忽略的问题: 使用<input />标签时容易忘记绑定bindblur()方法(输入框失去焦点时触发),因为用户用键盘输入时不一定会点击完成 ...

  6. JS高阶---语句分号相关

    [总结] 小括号和中括号开头的在其前必须加封号: [主体] 首先搜索下 [主体] (1)讨论---编码风格问题 (2)什么情况必须用封号? 1.其后跟着匿名式函数调用 此时可以在匿名函数前加:如下所示 ...

  7. python 函数,模块知识点运用示例

    给定验证码长度n,生成随机验证码,验证码由数字.字母组成(参考chr()内置方法) # 给定验证码长度n,生成随机验证码,验证码由数字.字母组成(参考chr()内置方法) # 第33-126号(共94 ...

  8. Push to origin/master was rejected

    在IDEA中往码云上传项目的时候出现了如下的错误:Push to origin/master was rejected 因为我是把代码上传到一个新的仓库里面,所以第一次提交的时候和仓库里面的东西不同步 ...

  9. NLP之预训练

    内容是结合:https://zhuanlan.zhihu.com/p/49271699 可以直接看原文 预训练一般要从图像处理领域说起:可以先用某个训练集合比如训练集合A或者训练集合B对这个网络进行预 ...

  10. [LeetCode] 854. K-Similar Strings 相似度为K的字符串

    Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two ...