MQTT客户端paho.mqtt.XXX

1. MQTT客户端C代码库

1.1 C源码下载构建

# centos7 OS 方法一
$ git clone https://github.com/eclipse/paho.mqtt.c.git
$ cd paho.mqtt.c
$ make
$ sudo make install
# make help查看当前Makefile支持命令
# make package 构造安装包压缩目录,解压缩后是生成项目链接库

mkdir -p build/output/samples
mkdir -p build/output/test
echo OSTYPE is Linux
OSTYPE is Linux
mkdir -p /usr/local/include
install -m 644  build/output/libpaho-mqtt3c.so.1.3 /usr/local/lib
install -m 644  build/output/libpaho-mqtt3cs.so.1.3 /usr/local/lib
install -m 644  build/output/libpaho-mqtt3a.so.1.3 /usr/local/lib
install -m 644  build/output/libpaho-mqtt3as.so.1.3 /usr/local/lib
install  build/output/paho_c_version /usr/local/bin
install  build/output/samples/paho_c_pub /usr/local/bin
install  build/output/samples/paho_c_sub /usr/local/bin
install  build/output/samples/paho_cs_pub /usr/local/bin
install  build/output/samples/paho_cs_sub /usr/local/bin
/sbin/ldconfig /usr/local/lib
ln -s libpaho-mqtt3c.so.1 /usr/local/lib/libpaho-mqtt3c.so
ln -s libpaho-mqtt3cs.so.1 /usr/local/lib/libpaho-mqtt3cs.so
ln -s libpaho-mqtt3a.so.1 /usr/local/lib/libpaho-mqtt3a.so
ln -s libpaho-mqtt3as.so.1 /usr/local/lib/libpaho-mqtt3as.so
install -m 644 src/MQTTAsync.h /usr/local/include
install -m 644 src/MQTTClient.h /usr/local/include
install -m 644 src/MQTTClientPersistence.h /usr/local/include
install -m 644 src/MQTTProperties.h /usr/local/include
install -m 644 src/MQTTReasonCodes.h /usr/local/include
install -m 644 src/MQTTSubscribeOpts.h /usr/local/include
install -m 644 src/MQTTExportDeclarations.h /usr/local/include
install -m 644 doc/man/man1/paho_c_pub.1 /usr/local/share/man/man1
install -m 644 doc/man/man1/paho_c_sub.1 /usr/local/share/man/man1
install -m 644 doc/man/man1/paho_cs_pub.1 /usr/local/share/man/man1
install -m 644 doc/man/man1/paho_cs_sub.1 /usr/local/share/man/man1

#方法二,cmake指定构建安装目录,作为第三方动态链接库
#将项目目录下载到/root下面,同时指定生成目标安装位置为/root/mqtt
mkdir -p root/mqtt/build
cd /root/mqtt/
cmake -DCMAKE_INSTALL_PREFIX="/root/mqtt" ../paho.mqtt.c
make
make install

编译成功后,生成4个二进制可执行文件,paho_c_pub paho_c_sub paho_cs_pub paho_cs_sub。c表示客户端,pub表示发布,sub表示订阅,cs表示安全openssl。还有4个动态链接库

libpaho-mqtt3a.so - asynchronous (MQTTAsync)
libpaho-mqtt3as.so - asynchronous with SSL (MQTTAsync)
libpaho-mqtt3c.so - "classic" / synchronous (MQTTClient)
libpaho-mqtt3cs.so - "classic" / synchronous with SSL (MQTTClient)

1.2 C客户端可执行文件使用

  • 可执行文件

命令行使用可参考 https://modelbasedtesting.co.uk/2018/08/08/paho-c-client-mqtt-5-0-and-command-line-utilities/

paho_c_sub -t topic -h 127.0.0.1 -p 1883  -q 2

paho_c_pub -t topic -h 127.0.0.1 -q 2 -m "hello,user"

1.3 排查问题

$ paho_c_pub
paho_c_pub: error while loading shared libraries: libpaho-mqtt3as.so.1: cannot open shared object file: No such file or directory

建议进入paho.mqtt.c目录中,重新make install一下。

2. MQTT客户端Golang代码库

官方示例:在官方库中存在样本,例如paho.mqtt.golang库示例在paho.mqtt.golang/cmd目录中,可以编译参考

2.1 GO客户端API

    //安装go mqtt客户端
    go get github.com/eclipse/paho.mqtt.golang
    go get github.com/gorilla/websocket
    go get golang.org/x/net/proxy
    //引入mqtt 客户端的API
    import "github.com/eclipse/paho.mqtt.golang"

2.2 GO客户端API

发表评论

邮箱地址不会被公开。 必填项已用*标注