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. css中的行内元素和块级元素总结

    块级元素 <address>,  <button>,  <caption>,  <dd>,  <del>,  <div>,  & ...

  2. 读《TCP/IP详解》:TCP

    TCP(Transmission Control Protocol,传输控制协议),位于传输层,提供一种面向连接.可靠的字节流服务. 字节流服务(Byte Stream Service)是指,为了方便 ...

  3. redo log重做日志缓冲

    ---------------------------------- 2015-02-10---------------------------------- innodb redo log (重做日 ...

  4. Centos7安装和配置Tomcat8

    第一步:下载Tomcat8压缩包 进入 http://tomcat.apache.org/download-80.cgi 下载tar.gz压缩包 第二步:用xshell工具把压缩包上传到/home/d ...

  5. 06点击事件 tabBar配置 拨打电话

    1== D:\wxxm 项目的地址 2==>tabBar在全局配置中 在pages的同级目录下创建images本地图标 (最好的是在远程获取img 因为微信是有大小限制的) selectedIc ...

  6. linux (08) nginx入门详解

    一. nginx 入门 nginx 入门学习 web服务器软件 windows IIS服务器 linux nginx apache 收费​lighthttp 公司的技术栈 收费版技术栈 apache ...

  7. Location配置与ReWrite语法(五)

    原文链接:https://www.cnblogs.com/crazylqy/p/6892010.html location表示uri方式定位,基础语法有三种: location = pattern { ...

  8. http协议里定义的四种常见数据的post方法

    原文 https://blog.csdn.net/charlene0824/article/details/51199292 关于http协议里定义的四种常见数据的post方法,分别是: applic ...

  9. python 面试真题

    0.Python是什么? Python是一种解释型语言.但是跟C和C的衍生语言不同,Python代码在运行之前不需要编译.其他解释型语言还包括PHP和Ruby. Python是动态类型语言,指的是在声 ...

  10. Windbg Locals(局部变量)窗口的使用

    在WinDbg中,可以通过输入命令.使用“局部变量”窗口或使用“监视”窗口查看局部变量.局部变量窗口显示当前作用域中的本地变量的所有信息. 如何打开Locals窗口 通过菜单View--->Lo ...