go ---MQTT client
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的更多相关文章
- 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 ...
- python mqtt client publish操作
使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic. #-*-coding:utf-8-*- import paho.mqtt ...
- mqtt client python example
This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...
- mqtt client api: 阻塞API
fusesource版本:mqtt-client-1.11.jar下载地址:https://github.com/fusesource/mqtt-client fusesource提供三种mqtt c ...
- MQTT Client library for C (MQTT客户端C语言库-paho)
原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/index.html 来自我的CSDN博客 最近在使用Paho的MQTT客 ...
- MQTT Server搭建(apache-apollo)和MQtt Client搭建
目标 本文就MQTT server和client搭建做以下总结,方便测试及开发使用,能基于MQTT软件发送和接收消息. 介绍 MQTT是基于tcp的消息发送,目前JAVA方面有两种实现,分别是mqtt ...
- php mqtt client
<?php /* phpMQTT */ class phpMQTT { private $socket; /* holds the socket */ private $msgid = 1; / ...
- Asynchronous MQTT client library for C (MQTT异步客户端C语言库-paho)
原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTAsync/html/index.html MQTT异步客户端C语言库 用于C的异步 MQTT 客 ...
- 一套权威的 MQTT Client 库
主流的语言都支持,可链接到 github ,亲测golang client 简单好用 http://www.eclipse.org/paho/downloads.php
随机推荐
- VMware基本用法
###VMware tools 介绍 只有在VMware虚拟机中安装好了VMware Tools,才能实现主机与虚拟机之间的文件共享,同时可支持自由拖拽的功能,鼠标也可在虚拟机与主机之前自由移动(不用 ...
- AccessCenter 模块结构
AccessCenter 模块结构
- [b0012] Hadoop 版hello word mapreduce wordcount 运行(二)
目的: 学习Hadoop mapreduce 开发环境eclipse windows下的搭建 环境: Winows 7 64 eclipse 直接连接hadoop运行的环境已经搭建好,结果输出到ecl ...
- ajax的jQuery的表单序列化获取参数serialize()
需要引入jQuery.js才能使用$("form表单的id").serialize()可获取form表单里面所有表单元素的值和name属性值,按顺序拼接成查询字符串格式为name值 ...
- MongoDB 4.X 用户和角色权限管理总结
关于MongoDB的用户和角色权限的梳理一直不太清晰,仔细阅读了下官方文档,并对此做个总结. 默认情况下,MongoDB实例启动运行时是没有启用用户访问权限控制的,也就是说,在实例本机服务器上都可以随 ...
- MATLAB之指定文件读取与读取地址输出
一.读取指定文件夹下的指定格式文件 (1) 利用命令 uigetdir('','') 参数解释: uigetdir('所要打开的盘地址','对打开的弹出框进行描述') 例如:uigetdir('C:\ ...
- MySQL8 修改密码验证插件
MySQL8 修改密码验证插件 查看当前用户使用的密码验证插件 mysql> show variables like '%auth%'; +--------------------------- ...
- maxima已知方程,计算结果
- Redis笔记1-Redis介绍及数据类型使用场景
Redis介绍:C语言开发.单线程操作.高性能.键值对.可持久化的数据库.Redis采用redisObject结构来统一五种数据类型,redisObject是五种类型的父类,可以在函数间传递时隐藏具体 ...
- async和await的用法
function fn(){ return "aa"; } console.log(fn())// 如果直接写个函数return一个值,那么打印的肯定就是aa async func ...